-
Notifications
You must be signed in to change notification settings - Fork 10k
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
Implement an alternative to startup running twice when using WebApplicationFactory #26487
Comments
Realized I forgot to tag you guys for continuity. |
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 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 dotnet-architecture/eShopOnWeb#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. #26487 (comment) still needs investigation. |
Thanks for contacting us. |
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
I upgraded to .NET 5 and am now also seeing |
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 |
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
The text was updated successfully, but these errors were encountered: