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();
Install the 'AutoMapper' and 'Microsoft.Extensions.DependencyInjection' package from NuGet.
Implement User and UserDTO class for mapping from User to UserDTO:
public class User
{
public int Id { get; set; }
public string Name { get; set; }
}
public class UserDTO
{
public int Id { get; set; }
public string Name { get; set; }
}
Implement an interface and a class for doing Mapping operation as follows(don't forget using AutoMapper):
public interface ICustomMapper<T>
{
T MapUserToUserDTO();
}
public class CustomMapper : ICustomMapper<UserDTO>
{
private readonly IMapper mapper;
public CustomMapper(IMapper mapper) =>
this.mapper = mapper;
public UserDTO MapUserToUserDTO()
{
User user = new User()
{
Id = 5,
Name = "Iman"
};
UserDTO userDTO = mapper.Map<UserDTO>(user);
return userDTO;
}
}
After that create a RegisterServices method for doing Dependency Injection in your project and a DisposeServices method for disposing of the service provider.
using AutoMapper;
using Microsoft.Extensions.DependencyInjection;
namespace AutoMapperProject
{
class Program
{
private static IServiceProvider _serviceProvider;
static void Main(string[] args)
{
RegisterServices();
ICustomMapper<UserDTO> service =
_serviceProvider.GetService<ICustomMapper<UserDTO>>();
UserDTO userDTO = service.MapUserToUserDTO();
DisposeServices();
Console.WriteLine("Hello AutoMapper!");
}
private static void RegisterServices()
{
var collection = new ServiceCollection();
collection.AddAutoMapper(typeof(Program));
collection.AddScoped<ICustomMapper<UserDTO>, CustomMapper>();
// ...
// Add other services
// ...
_serviceProvider = collection.BuildServiceProvider();
}
private static void DisposeServices()
{
if (_serviceProvider == null)
{
return;
}
if (_serviceProvider is IDisposable)
{
((IDisposable)_serviceProvider).Dispose();
}
}
}
}
Good Luck
You need to map a concrete class that implements IMapper.
I followed Automapper documentation and it worked fine, it would be more efficient to show their documentation than writing you code there and I don't want copy / paste their sample
http://automapper.readthedocs.io/en/latest/Dependency-injection.html
But it would be like this: services.AddTransient<IMapper,YourconcreteClass>();