我们有一个WCF服务的解决方案,使用EF 6从数据库获取数据。存储库DLL通过IoC容器注入。在此DLL中,您可以找到实体,上下文,AutoMapper配置配置文件等。
以下是SubscriberProfile的示例:
public class SubscriberProfile : Profile
{
protected override void Configure()
{
MapDbToDto();
}
private static void MapDbToDto()
{
Mapper.CreateMap<SubscriberDb, SubscriberDto>()
.ForMember(x => x.FieldA, opt => opt.MapFrom(x => x.Filed123.Valeur))
.ForMember(x => x.Contact, opt => opt.Condition(src => !Helpers.IsAllDefault(src.Contact)))
.ForMember(x => x.Conjoint, opt => opt.Condition(src => !Helpers.IsAllDefault(src.Conjoint)));
Mapper.CreateMap<AnotherDb, AnotherDto>();
Mapper.CreateMap<ContactDb, ContactDto>();
}
}
添加配置文件的配置文件:
public static class AutoMapperRepositoryConfiguration
{
//Create mapings only once
public static void Configure()
{
if (Mapper.GetAllTypeMaps().Any())
return;
Mapper.Configuration.DisableConstructorMapping();
Mapper.AddProfile(new ConventionProfile());
Mapper.AddProfile(new SubscriberProfile());
Mapper.AddProfile(new BeneficiaryProfile());
}
}
在每个repo构造函数中,调用Configure方法,因此它获取映射:
public SubscriberReadRepository(PlansContext context)
{
if (context == null)
throw new ArgumentNullException("context");
AutoMapperRepositoryConfiguration.Configure();
_context = context;
}
一切都很好,几乎所有时间。在服务器上,有时候,无处不在,我们会得到臭名昭着的&#34; 缺少类型的地图配置或不支持的映射&#34;错误:
System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]: Missing type map configuration or unsupported mapping.
Mapping types:
SubscriberDb -> SubscriberDto
MyRepo.SouscripteurDb -> MyRepo.SubscriberDto
Destination path:
SubscriberDto
Source value:
System.Data.Entity.DynamicProxies.SubscriberDb_0498438FE3D70326924850E4199183EC0EB2AC8DF509202F8CB4EF2D02D9E835 (Fault Detail is equal to An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Mapping types:
SubscriberDb -> SubscriberDto
MyRepo.SouscripteurDb -> MyRepo.SouscripteurDto
Destination path:
SubscriberDto
Source value:
System.Data.Entity.DynamicProxies.SubscriberDb_0498438FE3D70326924850E4199183EC0EB2AC8DF509202F8CB4EF2D02D9E835
at MyRepo.SubscriberReadRepository.Get(Int32 idSouscripteur) in e:\BuildAgent\work\9dd90bc72a81e0e3\Sources\MyRepo\SubscriberReadRepository.cs:line 59
at MyRepo.SubscriberReadServices.Get(Int32 id) in e:\BuildAgent5\work\9dd90bc72a81e0e3\Sources\MyRepo\SubscriberReadServices.cs:line 33
at WCF.Services.SubscriberServices.Get(Int32 id) in e:\BuildAgen...).
一旦我们收到错误,后续调用都不起作用,它们都会以相同的错误崩溃。然后我们转到IIS并重新启动WCF服务,一切都恢复正常。它可以工作几天没有问题然后它再次发生没有明显的原因。我们的DBA确保没有进行DB修改。有没有人有想法?我在这里和谷歌的任何地方都进行过检查,但无法找到像我们这样的随机内容。我在这里看到一篇帖子,Jimmy Bogard谈到了调用&#34; base.CreateMap&#34;取而代之的是&#34; Mapper.CreateMap&#34;但是还没有尝试过,因为我们甚至不知道为什么会发生这种错误。
答案 0 :(得分:0)
您正在调用错误的CreateMap方法。在Profile类中调用基础CreateMap方法:
public class SubscriberProfile : Profile
{
protected override void Configure()
{
MapDbToDto();
}
private static void MapDbToDto()
{
CreateMap<SubscriberDb, SubscriberDto>()
.ForMember(x => x.FieldA, opt => opt.MapFrom(x => x.Filed123.Valeur))
.ForMember(x => x.Contact, opt => opt.Condition(src => !Helpers.IsAllDefault(src.Contact)))
.ForMember(x => x.Conjoint, opt => opt.Condition(src => !Helpers.IsAllDefault(src.Conjoint)));
CreateMap<AnotherDb, AnotherDto>();
CreateMap<ContactDb, ContactDto>();
}
}
使用Initialize可以更好地进行初始化:
public static class AutoMapperRepositoryConfiguration
{
//Create mapings only once
public static void Configure()
{
if (Mapper.GetAllTypeMaps().Any())
return;
Mapper.Initialize(cfg => {
cfg.DisableConstructorMapping();
cfg.AddProfile(new ConventionProfile());
cfg.AddProfile(new SubscriberProfile());
cfg.AddProfile(new BeneficiaryProfile());
});
}
}