-
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
.NET Core Identity Works with Normal Startup but not WebHostFactory #24740
Comments
Its not clear exactly how you are setting up your unit test, but clearly something is calling AddAuthentication with the same identity scheme twice. Maybe try commenting out the call to AddIdentity to confirm that makes the error go away, if another scheme throws that exception then that method is getting called more than once |
Thanks @HaoK, it does work after commenting out the addauth lines. Agreed about it getting added twice, but I'm not sure how other than possibly the As for the text context, here it a get endpoint test: [Collection("Sequential")]
public class GetValueToReplaceIntegrationTests : IClassFixture<CustomWebApplicationFactory>
{
public GetValueToReplaceIntegrationTests(CustomWebApplicationFactory factory)
{
_factory = factory;
}
private readonly CustomWebApplicationFactory _factory;
[Fact]
public async Task GetValueToReplaces_ReturnsSuccessCodeAndResourceWithAccurateFields()
{
var fakeValueToReplaceOne = new FakeValueToReplace { }.Generate();
var fakeValueToReplaceTwo = new FakeValueToReplace { }.Generate();
var appFactory = _factory;
using (var scope = appFactory.Services.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<ValueToReplaceDbContext>();
context.Database.EnsureCreated();
//context.ValueToReplaces.RemoveRange(context.ValueToReplaces);
context.ValueToReplaces.AddRange(fakeValueToReplaceOne, fakeValueToReplaceTwo);
context.SaveChanges();
}
var client = appFactory.CreateClient(new WebApplicationFactoryClientOptions
{
AllowAutoRedirect = false
});
var result = await client.GetAsync($"api/ValueToReplaceLowers")
.ConfigureAwait(false);
var responseContent = await result.Content.ReadAsStringAsync()
.ConfigureAwait(false);
var response = JsonConvert.DeserializeObject<IEnumerable<ValueToReplaceDto>>(responseContent);
// Assert
result.StatusCode.Should().Be(200);
response.Should().ContainEquivalentOf(fakeValueToReplaceOne, options =>
options.ExcludingMissingMembers());
response.Should().ContainEquivalentOf(fakeValueToReplaceTwo, options =>
options.ExcludingMissingMembers());
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
} |
You are just going to have to go through things carefully, are you sure there isn't an auto included file somewhere you missed? |
Not that I'm seeing :-/ will try and dig into it deeper. thanks |
@pdevito3 feel free to reopen this if you have a consistent reproduction of the problem. |
Describe what isn't working as expected
So I'm setting up an API in .NET Core 3.1 that is working fine when I run it locally, but when I run my xUnit project, I get this
System.InvalidOperationException : Scheme already exists: Identity.Application
error.Now 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.For me, I'm not using
AddDefaultIdentity
and I'm not seeing aIdentityHostintgStartup.cs
get generated, so I'm not quite sure what the deal is here.Steps to reproduce
Running my integration tests should pass without the exception. When I remove the identity config from my startup and run my tests without it, they pass and the app runs fine with just a normal startup, so I'm guessing it's the way the the webhostfactory is running things.
Here is my Identity Service Registration that gets added to Startup
here's my startup
And here is my factory for my integration tests
Got Exceptions? Include both the message and the stack trace
Further technical details
EF Core version:
Database provider: Microsoft.EntityFrameworkCore.UseInMemoryDatabase
Target framework: .NET Core 3.1
Operating system:
IDE: Visual Studio 2019 16.6.5
The text was updated successfully, but these errors were encountered: