请参阅此帖子以获取解决方案。
好的,我终于想通了。我的代码中的 AppDomain.CurrentDomain.GetAssemblies() 片段有时无法获取我的映射程序集,因此当它丢失时,我会收到错误消息。通过强制应用程序查找所有程序集来替换此代码解决了我的问题。
我的实体:
/// <summary>
/// Get/Set the name of the Country
/// </summary>
public string CountryName { get; set; }
/// <summary>
/// Get/Set the international code of the Country
/// </summary>
public string CountryCode { get; set; }
/// <summary>
/// Get/Set the coordinate of the Country
/// </summary>
public Coordinate CountryCoordinate { get; set; }
/// <summary>
/// Get/Set the cities of the country
/// </summary>
public virtual ICollection<City> Cities
{
get
{
if (_cities == null)
{
_cities = new HashSet<City>();
}
return _cities;
}
private set
{
_cities = new HashSet<City>(value);
}
}
我的 DTO:
public Guid Id { get; set; }
public string CountryName { get; set; }
public string CountryCode { get; set; }
public string Lattitude { get; set; }
public string Longtitude { get; set; }
public List<CityDTO> Cities { get; set; }
我的配置
// Country => CountryDTO
var countryMappingExpression = Mapper.CreateMap<Country, CountryDTO>();
countryMappingExpression.ForMember(dto => dto.Lattitude, mc => mc.MapFrom(e => e.CountryCoordinate.Lattitude));
countryMappingExpression.ForMember(dto => dto.Longtitude, mc => mc.MapFrom(e => e.CountryCoordinate.Longtitude));
在 Global.asax Application_Start 我有:
Bootstrapper.Initialise();
在 Bootstrapper 我有:
public static class Bootstrapper
{
private static IUnityContainer _container;
public static IUnityContainer Current
{
get
{
return _container;
}
}
public static void Initialise()
{
var container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
private static IUnityContainer BuildUnityContainer()
{
_container = new UnityContainer();
_container.RegisterType(typeof(BoundedContextUnitOfWork), new PerResolveLifetimeManager());
_container.RegisterType<ICountryRepository, CountryRepository>();
_container.RegisterType<ITypeAdapterFactory, AutomapperTypeAdapterFactory>(new ContainerControlledLifetimeManager());
_container.RegisterType<ICountryAppService, CountryAppServices>();
EntityValidatorFactory.SetCurrent(new DataAnnotationsEntityValidatorFactory());
var typeAdapterFactory = _container.Resolve<ITypeAdapterFactory>();
TypeAdapterFactory.SetAdapter(typeAdapterFactory);
return _container;
}
}
我的适配器在哪里:
public class AutomapperTypeAdapter : ITypeAdapter
{
public TTarget Adapt<TSource, TTarget>(TSource source)
where TSource : class
where TTarget : class, new()
{
return Mapper.Map<TSource, TTarget>(source);
}
public TTarget Adapt<TTarget>(object source) where TTarget : class, new()
{
return Mapper.Map<TTarget>(source);
}
}
AdapterFactory 是:
public AutomapperTypeAdapterFactory()
{
//Scan all assemblies to find an Auto Mapper Profile
var profiles = AppDomain.CurrentDomain
.GetAssemblies()
.SelectMany(a => a.GetTypes())
.Where(t => t.BaseType == typeof(Profile));
Mapper.Initialize(cfg =>
{
foreach (var item in profiles)
{
if (item.FullName != "AutoMapper.SelfProfiler`2")
cfg.AddProfile(Activator.CreateInstance(item) as Profile);
}
});
}
所以我随机得到一个“缺少类型映射配置或不支持的映射”。错误提示:
public TTarget Adapt<TTarget>(object source) where TTarget : class, new()
{
return Mapper.Map<TTarget>(source);
}
虽然此错误是随机发生的,但很难调试并查看会发生什么。我已经搜索了很多没有适当的解决方案。
错误如下:
缺少类型映射配置或不支持的映射。
映射类型:Country -> CountryDTO MyApp.Domain.BoundedContext.Country -> MyApp.Application.BoundedContext.CountryDTO
目标路径:List`1[0]
源值:MyApp.Domain.BoundedContext.Country
我的项目是一个带有 Automapper 2.2 和 Unity IoC 的 MVC 3 项目。
我将不胜感激任何想法、建议或解决方案,并感谢您的回答。