I need to use a service layer in the AutoMapper
profile class in ASP.NET Core
but when I inject service in the constructor it does not work. For ex
It's better to use custom IValueResolver for this purposes because it is fully supports IServiceCollection integration (using AutoMapper.Extensions.Microsoft.DependencyInjection).
You may need to implement a custom value resolver:
public class UserViewModelValueResolver: IValueResolver<...>
{
public readonly IUserManager userManager;
public UserViewModelValueResolver(IUserManager userManager)
{
this.userManager = userManager;
}
...
}
And the registration in services may be reduced to:
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddSingleton();
}
Then you can get a mapper instance inside a controller by injecting IMapper
via a constructor.
Based on: AutoMapper: Handling Profile Dependencies using Custom Value Resolvers - Tech Net