I use the latest version of Automapper in my application, together with Autofac. I set the configurations and profiles and unit tested all my profiles with AssertIsConfigurationValid()
and everything is working fine.
However, when I use the mapper
within my application I get a "Automapper missing type map configuration or unsupported mapping"
exception only when running from code, I suspect it is something to do with how I set the mapper to work with Autofac:
// This is how I register my mapper with Autofac
public class ModelsMapperModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterAssemblyTypes().AssignableTo(typeof(Profile)).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();
}
}
// Here is a simple version of my objects and mappings:
public class LetterDomain
{
public List<StationDomain> Stations {get; set;}
public string Title {get; set;}
public int Id {get; set;}
public int TimeCreated {get; set;}
public string File {get; set;}
public bool IsSecret {get; set;}
}
public class StationDomain
{
public int Id {get; set;}
public string Owner {get; set;}
public string Name {get; set;}
}
public class LetterDto
{
public DestinationDto Dest {get; set;}
public int Id {get; set;}
}
public class DestinationDto
{
public List<StationDto> Stations {get; set;}
}
public class StationDto
{
public string Name {get; set;}
}
public class MyProfile : Profile
{
protected override void Configure
{
CreateMap<StationDomain, StationDto>()
CreateMap<LetterDomain, DestinationDto>();
CreateMap<LetterDomain, LetterDto>()
.ForMember(x => x.Dest, opt => opt.MapFrom(src => Mapper.Map<DestinationDto>(src)));
}
}
public void MyMethodInsideApplication(LetterDomain letter)
{
// Exception is thrown here
var dto = _mapper.Map<LetterDto>(letter);
}
I'm trying to map LetterDomain
to LetterDto
in my application and it tells me that the configuration of LetterDomain
to DestinationDto
is missing, although I definitely created the mapping..
Would really like for some help here..
Thanks in advance!
Edit: I just wanna add that all other mappings that doesn't use the static Mapper.Map<> inside their configuration profile work good in the application