我在我的應用程序中使用最新版本的Automapper和Autofac。我設置了配置和配置文件,並且單元測試了我的所有配置文件AssertIsConfigurationValid()
,並且一切正常。 然而,當我用我的應用程序中的映射 我得到一個"Automapper missing type map configuration or unsupported mapping"
例外,只是從代碼中運行時,我懷疑這是值得做的怎麼設置映射器與Autofac工作:設置Autofac與Automapper配合使用的問題
// 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);
}
我米試圖映射LetterDomain
在我的應用程序LetterDto
,它告訴我的LetterDomain
到DestinationDto
配置丟失,但我絕對創建的映射..
真的想在這裏有些幫助..
提前致謝!
編輯: 我只想補充一點,這並不在應用程序中使用靜態Mapper.Map <>的配置文件工作的良好內的所有其他映射
在哪個班沒有了'MyMethodInsideApplication'法活嗎?這樣的類是通過容器創建的嗎?你能說明一下嗎? –
這是一個單例,它在startapp中解析 –
你在哪裏使用靜態的'Mapper.Map <>'方法?你可以顯示該代碼嗎? –