Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebHostBuilderExtensions Configure can result in HostingStartupAssemblies being run twice #31382

Closed
k-s-s opened this issue Mar 30, 2021 · 7 comments · Fixed by #31440
Closed
Assignees
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions bug This issue describes a behavior which is not expected - a bug.
Milestone

Comments

@k-s-s
Copy link

k-s-s commented Mar 30, 2021

Describe the bug

Background

I am using IHostingStartup within my package library as it is used by external users to create the web host, targets NET 4.6 and NET Core 3.1. I set the builder setting HostingStartupAssembliesKey to package library assembly FullName
I have added a feature to add use an instanced startup class instead of using the normal UseStartup. It is a custom interface, not Microsoft.AspNetCore.Hosting.IStartup
The intention is for testing, to make an easier way of testing different services, rather than separate startup classes.

Bug

  1. When using the Microsoft.AspNetCore.Hosting.WebHostBuilderExtensions.Configure method (I add the method of the instanced startup), the extension method sets WebHostBuilder setting ApplicationKey to startup assembly. This was not correct as it was setting to the package library, using assembly Name
    public static IWebHostBuilder Configure(this IWebHostBuilder hostBuilder, Action<WebHostBuilderContext, IApplicationBuilder> configureApp)
  2. When I do host.Build(), the method:
    private IServiceCollection BuildCommonServices(out AggregationException hostingStartupErrors)
    create WebHostOptions, which registers 2x hosting startup assemblies because of Name and FullName

To Reproduce

  1. Use IHostingStartup, using assembly FullName
  2. Use WebHostBuilder.Configure
  3. Run WebHost.Build()

Exceptions (if any)

I found the issue that Swashbuckle raised an exception that a document was already registered.

Further technical details

  • NET 4.6.1, NET Core 3.1
@davidfowl
Copy link
Member

@k-s-s do you have a sample application?

@davidfowl
Copy link
Member

davidfowl commented Apr 1, 2021

Trying to interpret what you put in the issue, this is what I came up with so far. I know I'm missing something 😄 .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

[assembly: HostingStartup(typeof(WebApplication50.HostingStartup))]

namespace WebApplication50
{
    public class HostingStartup : IHostingStartup
    {
        public void Configure(IWebHostBuilder builder)
        {
            
        }
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseSetting(WebHostDefaults.HostingStartupAssembliesKey, typeof(Program).Assembly.FullName)
                              .Configure(new Startup().Configure);
                });
    }
}

@k-s-s
Copy link
Author

k-s-s commented Apr 1, 2021

Hi David

That looks about right, if you put a breakpoint on the HostingStartup.Configure, I think it should call twice. In the HostingStartup.Configure method, to check, register singleton, that links to a static variable and will throw exception if already set.
If it doesn't call twice, then try as well to call this from a separate project, with a startup from that project.

I will make a sample this weekend.

@davidfowl
Copy link
Member

No need, I can reproduce the problem and will have a fix out ASAP.

@davidfowl davidfowl added bug This issue describes a behavior which is not expected - a bug. and removed investigate labels Apr 1, 2021
@ghost ghost locked as resolved and limited conversation to collaborators May 1, 2021
@davidfowl
Copy link
Member

@Tratcher should we back port this?

@Tratcher
Copy link
Member

Tratcher commented Aug 2, 2021

@Tratcher should we back port this?

Why? This one report doesn't seem to justify that.

@davidfowl
Copy link
Member

It's not about the number of reports, it's about the potential impact, and re-reading this issue again I don't think its common to set a different assembly name like this.

@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Aug 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions bug This issue describes a behavior which is not expected - a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants