Currently we have almost the same problem, that is described here #2399.
Service Fabric tries to initialize an application and it has already executed ConfigureServices() method successfully and is trying to execute Configure().
In rare cases Configure method can throw an exception. (and it still is fine, as our service will be restarted by SF) So SF restarts the app and is doing it just in the same process. Which means it is trying to execute ConfigureServices() method again.
And here we go:
Mapper already initialized. You must call Initialize once per application domain/process.
at AutoMapper.Mapper.set_Configuration(IConfigurationProvider value)
at AutoMapper.Mapper.Initialize(Action1 config) at AutoMapper.ServiceCollectionExtensions.AddAutoMapperClasses(IServiceCollection services, Action1 additionalInitAction, IEnumerable`1 assembliesToScan)
at myITprocess.StrategyApi.Startup.ConfigureServices(IServiceCollection services)
We've fixed it by setting
Mapper.Reset();
services.AddAutoMapper();
but the solution seems to be a bit of a hack.
You should switch to the instance based API. See this.
Haven't seen this yet. Thanks a lot
Mapper.Reset() tries to set null to the instance properties and fails as they are instantiated already. I would suggest by the way to add these improvements:
There is no safe way to reset the mapper. You should switch to the instance based API.
How then to setup Automapper in Test Environment in the startup.cs? I have the code here:
if (_env.EnvironmentName == "Test")
{
//this returns Mapper already initialized in my tests.
services.AddAutoMapper(typeof(Startup));
}
else
{
services.AddAutoMapper(typeof(Startup));
}
A static constructor works.
On Sat, Dec 2, 2017 at 6:13 AM Miha Jakovac notifications@github.com
wrote:
How then to setup Automapper in Test Environment in the startup.cs? I have
the code here:
if (_env.EnvironmentName == "Test") { //this returns Mapper already
initialized in my tests. services.AddAutoMapper(typeof(Startup)); } else {
services.AddAutoMapper(typeof(Startup)); }—
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/2413#issuecomment-348688076,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAGYMh4GB4ku-Bfo-i-_Qv5v1LOg-msXks5s8T7fgaJpZM4QleVc
.
I tried with
Mapper.Initialize(cfg => {
cfg.AddProfile<MyProfile>();
});
The same thing. I also tried to add Mapper.Reset() before it.
in my startup.cs in MVC solution I have:
if (_env.EnvironmentName == "Test")
{
Mapper.Reset();
Mapper.Initialize(cfg =>
{
cfg.AddProfile<ApiMapperProfile>();
});
}
else
{
services.AddAutoMapper(typeof(Startup));
}
And in xUnit TestFixture:
public readonly TestServer _testServer;
public HttpClient Client { get; }
public TestServerFixture()
{
_testServer = new TestServer(new WebHostBuilder()
.UseContentRoot(GetContentRootPath())
.UseEnvironment("Test")
.UseStartup<API.Startup>());
Client = _testServer.CreateClient();
Client.BaseAddress = new Uri("http://localhost");
}
public void Dispose()
{
Client.Dispose();
_testServer.Dispose();
}
And I still get the error, of (Mapper already initialized. You must call Initialize once per application domain/process.) (The following constructor parameters did not have matching fixture data: TestServerFixture fixture
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 should switch to the instance based API. See this.