I try to use AutoMapper in my .Net Core 2.0 Console Application. My class looks like this:
public class AutoGetCurrency
{
private readonly IMapper mapper;
public AutoGetCurrency(IMapper mapper) {
this.mapper = mapper;
}
....
}
I tried to add services into Main method:
static void Main(string[] args)
{
var services = new ServiceCollection();
ServiceProvider = services.BuildServiceProvider();
services.AddTransient<IMapper>();
....
}
But I have the next error 'One or more errors occurred. (Object reference not set to an instance of an object.)'
Please tell me how to add AutoMapper to use DI.
You need to add this nuget package AutoMapper.Extensions.Microsoft.DependencyInjection to your project and add AutoMapper
to your ServiceCollection
like this
var services = new ServiceCollection();
services.AddAutoMapper(mapperConfig => mapperConfig.AddProfiles(GetType().Assembly))
ServiceProvider = services.BuildServiceProvider();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With