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, Action1 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.TestServerFixture1..ctor() in C:\My\src\My\Tests\Fixtures\TestServerFixture.cs:line 27
Yes, you should switch to the object based API.
Could you help me with link ? Thanks
I have replaced the call services.AddAutoMapper(cfg => cfg.AddProfile
with following
_mapperConfiguration = new MapperConfiguration(cfg => cfg.AddProfile
services.AddAutoMapper();
where _mapperConfiguration is private static field in the Startup .
Still get the same error in tests. Where is my mistake ?
I think AddAutoMapper uses the static Mapper to do its job, you need to create a mapper using the newly created configuration.
You're right .
services.AddSingleton
new Mapper(new MapperConfiguration(cfg => cfg.AddProfile
works as a magic .
So there is an issue is with AutoMapper.Extensions.Microsoft.DependencyInjection .
Thank your help.
@jbogard I think AM.DI should be able to work with a MapperConfiguration object.
You shouldn’t call services.AddAutoMapper multiple times. That should just
be called once at the start of all your tests.
On Tue, Nov 14, 2017 at 12:48 PM petertsu notifications@github.com wrote:
I have replaced the call services.AddAutoMapper(cfg => cfg.AddProfile());
in the Startup.cswith following
_mapperConfiguration = new MapperConfiguration(cfg => cfg.AddProfile());
services.AddAutoMapper();where _mapperConfiguration is private static field in the Startup .
Still get the same error in tests. Where is my mistake ?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/AutoMapper/AutoMapper/issues/2399#issuecomment-344219791,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAGYMl6e7uPLFL_GsztxxBNl3Q0Y0packs5s2XAIgaJpZM4QdDZ8
.
I call the services.AddAutoMappe one time in the Startup.cs.
In my test project , I have used the Microsoft.AspNetCore.TestHost.TestServer to test an api . I have created the xUnit TestFixture which creates Microsoft.AspNetCore.TestHost.TestServer . The TestFixture is shared between tests in the same test class . Bottom line the TestFixture is created per test class and this is Ok by me , since I would like to inject different mocks in the different test classes
Hmmmm I’ve done this before with child containers instead of this...since
calling configure is expensive.
Does their DI let you do that?
On Tue, Nov 14, 2017 at 2:21 PM petertsu notifications@github.com wrote:
I call the services.AddAutoMappe one time in the Startup.cs.
In my test project , I have used the
Microsoft.AspNetCore.TestHost.TestServer to test an api . I have created
the xUnit TestFixture which creates
Microsoft.AspNetCore.TestHost.TestServer . The TestFixture is shared
between tests in the same test class . Bottom line the TestFixture is
created per test class and this is Ok by me , since I would like to inject
different mocks in the different tests—
You are receiving this because you were mentioned.Reply to this email directly, view it on GitHub
https://github.com/AutoMapper/AutoMapper/issues/2399#issuecomment-344242652,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAGYMoVYRMCCZYkeKVh1_9Kno1wrhdEIks5s2YXDgaJpZM4QdDZ8
.
As far as I know the default DI of the .NET Core doesn't support child containers
No, it's doesn't. We'll need to add back Mapper.Reset.
Sorry for not understanding, but how did you solve the problem? Can you use autoMapper and run all test ?
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
Hi Linuxninja39 I was having the exact same issue as you . If you have a profile set up then you can do the following as stated above as this worked for me not ideal but hey
services.AddSingleton(new Mapper(new MapperConfiguration(cfg => cfg.AddProfile
Where DutchMappingProfile is the class for your profile you have set up eg
using AutoMapper;
using DutchTreat.Data.Entities;
using DutchTreat.ViewModels;
namespace DutchTreat.Data
{
public class DutchMappingProfile : Profile
{
public DutchMappingProfile()
{
CreateMap
.ForMember(o => o.OrderId, ex => ex.MapFrom(o => o.Id));
CreateMap<OrderViewModel, OrderItemViewModel>()
.ReverseMap();
}
}
}
@mquelcutti Your solution worked for me. Except I suspect most people will be injecting IMapper, so this is what I have:
services.AddSingleton<IMapper>(new Mapper(new MapperConfiguration(cfg => cfg.AddProfile<OneOfMyProfileClasses>())));
The major downside is that you'll have to manually add the profiles (with cfg.AddProfile) yourself instead of AutoMapper discovering/adding them for you like it does with services.AddAutoMapper();
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 do services.AddAutoMapper
On Fri, Jan 5, 2018 at 3:06 PM, John-Luke Laue notifications@github.com
wrote:
@mquelcutti https://github.com/mquelcutti Your solution worked for me.
Except I suspect most people will be injecting IMapper, so this is what I
have:services.AddSingleton
(new Mapper(new MapperConfiguration(cfg =>
cfg.AddProfile()))); The major downside is that you'll have to manually add the profiles (with
cfg.AddProfile) yourself instead of AutoMapper discovering/adding them for
you like it does with services.AddAutoMapper();—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
https://github.com/AutoMapper/AutoMapper/issues/2399#issuecomment-355665502,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAGYMm4WtvT9IO7lHa96_6ruJRoW6JUOks5tHo7jgaJpZM4QdDZ8
.
Ok thanks will give it a go
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?
We're seeing the same behaviour. Running multiple xunit test fixtures services.AddAutoMapper(); throws already initialized exception regardless of whether Mapper.Reset() is called before services.AddAutoMapper() or not.
@mrmkamranmalik Don't call Mapper.Initialize 1700 places. That's always been a bad idea, now we enforce it.
@david-waterworth Don't call it more than once, services.AddAutoMapper should only be called once. You can put that part in a static constructor or similar to make sure it's only called once.
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?
The other option is to not use the static version.
On Thu, May 3, 2018 at 5:33 PM David Waterworth notifications@github.com
wrote:
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 so the aspnet service
startup gets called multiple times. I think the workaround might be to
create the TestServer and TestClients as statics in the test assembly—
You are receiving this because you modified the open/close state.Reply to this email directly, view it on GitHub
https://github.com/AutoMapper/AutoMapper/issues/2399#issuecomment-386457070,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAGYMrHFo9Te9qH-qg8GlfwolVeESvnhks5tu4XRgaJpZM4QdDZ8
.
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?
Calling "AutoMapper.ServiceCollectionExtensions.UseStaticRegistration = false;" before services.AddAutoMapper solved the issue for me.
Without that my integration tests with TestServer using fixtures throws this:
Failed Taksapp.Web.IntegrationTests.Api.v1.Controllers.PassengerControllerTests.SignUpShould.ReturnForbiddenGivenIncorrectVerificationCode
Error Message:
System.AggregateException : One or more errors occurred. (Mapper already initialized. You must call Initialize once per application domain/process.) (The following constructor parameters did not have matching fixture data: Fixture2 fixture) ---- System.InvalidOperationException : Mapper already initialized. You must call Initialize once per application domain/process. ---- The following constructor parameters did not have matching fixture data: Fixture2 fixture
Stack Trace:----- Inner Stack Trace #1 (System.InvalidOperationException) -----
at AutoMapper.Mapper.set_Configuration(IConfigurationProvider value)
at AutoMapper.Mapper.Initialize(Action1 config) at AutoMapper.ServiceCollectionExtensions.AddAutoMapperClasses(IServiceCollection services, Action1 additionalInitAction, IEnumerable1 assembliesToScan) at Taksapp.Web.Startup.ConfigureServices(IServiceCollection services) in /Users/henrick/Projects/Taksapp/Taksapp.Web/Startup.cs:line 45 --- 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.Initialize() at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build() at Microsoft.AspNetCore.TestHost.TestServer..ctor(IWebHostBuilder builder, IFeatureCollection featureCollection) at Taksapp.Web.IntegrationTests.Fixtures.Fixture2..ctor() in /Users/henrick/Projects/Taksapp/Taksapp.Web.IntegrationTests/Fixtures/Fixture.cs:line 51
----- Inner Stack Trace #2 (Xunit.Sdk.TestClassException) -----
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.
@VictorioBerra I think you're right - we switched from XUnit to MSTest to work around this
@david-waterworth check this out, I was able to finally understand it all and get it working.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
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.Resetright before you doservices.AddAutoMapperOn Fri, Jan 5, 2018 at 3:06 PM, John-Luke Laue notifications@github.com
wrote: