AutoMapper: After upgrade to 6.2.0 - xUnit test fixtures throw exception
Hi , After upgrade from 6.1.1 to 6.2.0 i get the following “Mapper already initialized. You must call Initialize once per application domain/process.”.
I have a xunit test fixture for Microsoft.AspNetCore.TestHost.TestServer and it used in many tests
at AutoMapper.Mapper.set_Configuration(IConfigurationProvider value)
at AutoMapper.Mapper.Initialize(Action1 config) at AutoMapper.ServiceCollectionExtensions.AddAutoMapperClasses(IServiceCollection services, Action
1 additionalInitAction, IEnumerable1 assembliesToScan) at My.Startup.ConfigureServices(IServiceCollection services) in C:\My\src\My\Startup.cs:line 37 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services) at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build() at Microsoft.AspNetCore.TestHost.TestServer..ctor(IWebHostBuilder builder, IFeatureCollection featureCollection) at My.Tests.Fixtures.TestServerFixture
1…ctor() in C:\My\src\My\Tests\Fixtures\TestServerFixture.cs:line 27
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 29 (10 by maintainers)
You don’t have to do that any more, the AutoMapper.Extensions.Microsoft.DependencyInjection project has configuration for this:
AutoMapper.ServiceCollectionExtensions.UseStaticRegistration = false;
That will then do all the other stuff.
Or you can call
Mapper.Reset
right before you doservices.AddAutoMapper
On Fri, Jan 5, 2018 at 3:06 PM, John-Luke Laue notifications@github.com wrote:
I’m running into this issue only when I’m using services.AddAutoMapper(typeof(Startup)) in Startup. I’m only calling it once. This works great when just running the server. However, when I try to use entity framework core database and migrations commands it dies with the “Initialize once” error. If I then remove that line, the database and migrations work fine, but the service will fail with a not initialized error
The issue is we’re running xunit to test an dotnet core aspnet project and the TestServer is being created each time a test runs (because we inject a DbContext which contains data required for the specific test using the InMemory db). So services.AddAutoMapper() has to be called multiple times?
I am having the exact same issue as @david-waterworth I am also using XUnit which I suspect may be the culprit. We need to find a way to fix this.
I didn’t think we were, originally we just called services.AddAutoMapper() which I assumed was creating it’s own instance. But then it started failing so we added Mapper.Reset() before it. Is there an example of how to configure AutoMapper to not use the static version in aspnet core 2.0?
i am using 1700 places where i use Mapper.Initialize, before updating automapper its work fine but now it throws mapper already initialized exception, any solution please?