How to configure Automapper in Structuremap

139 Views Asked by At

Can someone guide me on how I configure Automapper in Structuremap? Currently, I have this service that will be using the IMapper.

public class RequestService : IRequestService
{
    private readonly IMapper _mapper;
    private readonly IRepositoryWrapper _repositoryWrapper;

    public RequestService(IMapper mapper, IRepositoryWrapper repositoryWrapper)
    {
        _mapper = mapper;
        _repositoryWrapper = repositoryWrapper;
    }

    public void GetSomething()
    {
        var result = _repositoryWrapper.RequestRepository.GetAll();
        _mapper.Map<RequestDto>(result);
    }
}

On the other hand, this is my Registry. This is also the place where I configure my automapper.

public class InfrastructureRegistry : Registry
{
    public InfrastructureRegistry()
    {          
        var mapperConfiguration = new MapperConfiguration(cfg => {
            cfg.AddProfile(typeof(MapperProfile));
        });

        var mapper = mapperConfiguration.CreateMapper();

        For<IMapper>().Use(mapper); 

        Scan(
            scan => {
                scan.TheCallingAssembly();
                scan.AssemblyContainingType<IRequestService>();
                scan.AssemblyContainingType<IRepositoryWrapper>();
                scan.WithDefaultConventions();
            });
        For<IRequestService>().Use<RequestService>();
        For<IRepositoryWrapper>().Use<RepositoryWrapper>();
        
    }
}

During testing, I get this error message. StructureMap.StructureMapConfigurationException: 'No default Instance is registered and cannot be automatically determined for type 'AutoMapper.IMapper'

I'm using the below version of:

  • Automapper = 10.1.1
  • StructureMap.WebApi2 = 3.0.4.125
  • StructureMap = 3.0.4.125

Hope someone can guide me on this.

TIA!

0

There are 0 best solutions below