Open pdevito3 opened 4 years ago
Realized I forgot to tag you guys for continuity.
@Tratcher @davidfowl @anurse
I'm confused by your base assertion here that Startup is running twice. That's not what the comments in the linked issue say.
You have multiple ConfigureServices methods and one runs after the other, but no single ConfigureServices method is being run twice, correct?
@Tratcher yeah, let me try to clarify.
In this instance and the linked instance, my code runs fine when doing a normal startup, but when running integration tests, it would seem that the entire ConfigureServices method is running twice, i.e. if I put a breakpoint on, any line in the AddIdentityInfrastructure
method, it will get hit twice when running WebApplicationFactory
.
Maybe I'm missing something with the proper configuration and operation of setting up integration tests, but even if that's the case, it isn't very intuitive as is. My point for the feature request is that, it would be great if we could have an alternative setup to just call a separate startup (e.g. StartupTesting) and call it a day. Simple and easy. No need to override anything in another startup and potentially run things multiple times.
Ok, we'll have to investigate that, it shouldn't run twice.
On another note, never call BuildServiceProvider, it messes up the DI service lifetimes. That DB initialization needs to happen later after the host/container get built.
Thanks, here's an example repo if you want to check out the bug in action.
Good tip, I'll be sure to update the BuildServiceProvider code. For what it's worth, the factory is based on eShopWeb, so a lot of people are probably getting bad info here. Maybe another good example that setting up the factory can be hard to wrap your head around for much of the community and having an option to have it work from a standard startup would be beneficial.
Thanks for the warning, I've filed https://github.com/dotnet-architecture/eShopOnWeb/issues/465 to clean up the sample.
Thanks, will be sure to update this if I come across any other instances.
This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.
See our Issue Management Policies for more information.
@mkArtakMSFT this isn't resolved. https://github.com/dotnet/aspnetcore/issues/26487#issuecomment-703611001 still needs investigation.
Thanks for contacting us.
We're moving this issue to the Next sprint planning
milestone for future evaluation / consideration. We will evaluate the request when we are planning the work for the next milestone. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.
All I want from WebApplicationFactory
is:
Startup.ConfigureServices
.Startup.ConfigureServices
is called only once.I upgraded to .NET 5 and am now also seeing Startup.ConfigureServices
called twice. I was using a TestStartup
class inheriting from Startup
to be able to override the ConfigureServices
method and achieve Number 1 above which was working nicely. Changes to the WebApplicationFactory
API has broken my code in every upgrade of .NET unfortunately. I don't really care how but is it possible to achieve the above two requirements?
I am having the exact same problem as @RehanSaeed I would expect if you override ConfigureWebHost in WebApplicationFactory and set UseStartup to a TestStartup class that the SUT Startup would not have ConfigureServices called as normal.
Any updates on this?
It seems like the issue is still there, are there known workarounds?
I doubt this is the reason for the problem mentioned in the original post, but in case anybody stumbles on this seeing the behavior I saw: I saw the Program code in a .NET 6 app getting executed twice because I had a WebApplicationFactory
whose code references this.Server
(as in clientBuilder.ConfigurePrimaryHttpMessageHandler(() => this.Server.CreateHandler())
), but my tests applied further customizations via WithWebHostBuilder()
, which provides a different instance of WebApplicationFactory. The call to this.Server
caused the wrapped factory instance to spin up its own host. Changing that to clientBuilder.ConfigurePrimaryHttpMessageHandler(services => ((TestServer)services.GetRequiredService<IServer>()).CreateHandler())
fixed that problem.
So, I know it's by design, but I’m running into another instance where startup getting called twice when using
WebApplicationFactory
is causing me major headaches.I added some code for this specific instance below, but the short version is that when I’m adding Auth into my API, it runs fine when doing startup normally, but when using the web host factory it's messing up my auth setup with a
System.InvalidOperationException : Scheme already exists: Identity.Application error.
error.New Feature Request
Maybe I’m just not getting the best way to override things, but in my mind it makes more sense to have (at the the option of using) a distinct StartupTesting or something of that nature that can be run once to configure my testing host exactly how I want. This is how Laravel does it an it seems more manageable. Related to #19404
Details on this particular error
When using Auth, the API will run fine, but the integration tests will break, throwing a
-------- System.InvalidOperationException : Scheme already exists: Identity.Application
error.I started googling for this and it seems like the main resolution is generally to remove
AddDefaultIdentity
to either stop a clash withIdentityHostingStartup
or prevent IdentityHostintgStartup.cs from causing some overlap.I'm not using AddDefaultIdentity and I'm not seeing a IdentityHostintgStartup.cs get generated, so I'm not quite sure what the deal is here. Presumably, something is calling
AddAuthentication
with the same identity scheme twice. This may be be due toCustomWebApplicationFactory
running through startup multiple times, but I need to investigate more.It does look like, when debugging any integration test that
services.AddIdentity<ApplicationUser, IdentityRole>().AddEntityFrameworkStores<IdentityDbContext>().AddDefaultTokenProviders();
is getting hit twice and, when commenting that line out, I get a different error:-------- System.InvalidOperationException : Scheme already exists: Bearer
which, again, is presumably happening because of startup getting run twice inCustomWebApplicationFactory
.WebAppFactory
Startup
Identity Extension