Simon Price
Simon Price

Reputation: 3261

AutoMapper 9.0 : AutoMapperMappingException: 'Missing type map configuration or unsupported mapping.'

I am working with a WCF Service and have setup DI with AutoFac and also wish to use automapper. I believe I have the injection(s) correct as I can see my configurations when I get into the service wish to call.

In my global.asax file I have this configuration;

protected void Application_Start(object sender, EventArgs e)
    {
        var builder = new ContainerBuilder();
        builder.RegisterType<ProposalService>().As<IProposalService>();
        builder.RegisterType<ProposalListService>().As<IProposalListService>();
        builder.RegisterType<ProposalContext>().As<IProposalContext>();
        builder.RegisterType<GetProposalListRequestProfile>().As<Profile>();
        builder.Register(c => new MapperConfiguration(cfg =>
        {
            foreach (var profile in c.Resolve<IEnumerable<Profile>>())
            {
                cfg.AddProfile(profile);
            }

        })).AsSelf().SingleInstance();

        builder.Register(c => c.Resolve<MapperConfiguration>().CreateMapper(c.Resolve)).As<IMapper>().InstancePerLifetimeScope();


        AutofacHostFactory.Container = builder.Build();
    }

In the Service I wish t use I have this

public class ProposalListService : IProposalListService
{
    private IProposalContext _proposalContext;
    private IMapper _mapper;

    public ProposalListService(IProposalContext proposlContext, IMapper mapper)
    {
        _proposalContext = proposlContext;
        _mapper = mapper;
    }

    public getProposalListResponse GetProposalList(getProposalListRequest request)
    {
        var mappedRequest = _mapper.Map<getProposalListRequest, Data.Models.getProposalListRequest>(request);
        var result = _proposalContext.GetProposalList(mappedRequest);
        var mappedResponse = _mapper.Map<Data.Models.getProposalListResponse, getProposalListResponse>(result);
        return mappedResponse;
    }
}

And the mapping profile I have this

public class GetProposalListRequestProfile : Profile
{
    public GetProposalListRequestProfile()
    {
        IMappingExpression<Data.Models.getProposalListRequest, getProposalListRequest>
            mappingExpression =
                CreateMap<Data.Models.getProposalListRequest, getProposalListRequest>()
                    .ForMember(dest => dest.getProposalList, opt => opt.MapFrom(src => src.getProposalList));

    }
}

The structure is

The WCF Service is going out to a class library and is then going to call EF to get the data, however when I get to line var mappedRequest = _mapper.Map<getProposalListRequest, Data.Models.getProposalListRequest>(request); I get error

AutoMapper.AutoMapperMappingException: 'Missing type map configuration or unsupported mapping.'

I would be grateful for any and all help on resolving this issue please.

Thanks

Upvotes: 0

Views: 501

Answers (1)

Guru Stron
Guru Stron

Reputation: 141665

If you will check the CreateMap method you will see next:

IMappingExpression<TSource, TDestination> CreateMap<TSource, TDestination>();

So your CreateMap<Data.Models.getProposalListRequest, getProposalListRequest>() will create map from Data.Models.getProposalListRequest to getProposalListRequest so to handle your use-case you will either add the reverted registration or try adding .ReverseMap() call after CreateMap.

Upvotes: 1

Related Questions