一切正常,直到我使用 NuGet 更新 CastleWinsor 和 AutoMapper 到其最新版本的引用:Castle.Windsor.3.0.0.4001
和AutoMapper.2.0.0
.
我有一个 AutoMapper 配置文件列表,与AutoMapperInstaller : IWindsorInstaller
. 它们位于不同的命名空间中,但这不重要,对吧?
这是一个配置文件示例:
namespace FieldService.Web.Mappings
{
public class RoleMappings : Profile
{
protected override void Configure()
{
AutoMapper.Mapper.CreateMap<RoleModel, Role>()
.ConstructUsing((role) => new Role() { Permissions = new List<Permission>() })
.ForMember(m => m.Permissions, o => o.MapFrom(src => src.Permissions.Where(p => p.Selected == true)));
}
}
}
这里是AutoMapperInstaller
namespace FieldService.Web.Infrastructure.IOC
{
public class AutoMapperInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
Mapper.Initialize(x => x.ConstructServicesUsing(container.Resolve));
RegisterProfilesAndResolvers(container);
RegisterMapperEngine(container);
}
private void RegisterMapperEngine(IWindsorContainer container)
{
container.Register(
Component.For<IMappingEngine>().Instance(Mapper.Engine)
);
}
private void RegisterProfilesAndResolvers(IWindsorContainer container)
{
// register value resolvers
container.Register(AllTypes.FromAssembly(Assembly.GetExecutingAssembly()).BasedOn<IValueResolver>());
// register profiles
container.Register(AllTypes.FromThisAssembly().BasedOn<Profile>());
var profiles = container.ResolveAll<Profile>();
foreach (var profile in profiles)
Mapper.AddProfile(profile);
}
}
}
在Global.asax
我有BootstrapContainer
我从方法调用的Application_Start
方法:
private static readonly IWindsorContainer container = new WindsorContainer();
public IWindsorContainer Container
{
get { return container; }
}
private static void BootstrapContainer()
{
container.Install(FromAssembly.This());
}
我得到的例外是:Trying to map xxx to yyyModel. Missing type map configuration or unsupported mapping. Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
我调试了安装程序,我认为这条线Container.Register(AllTypes.FromThisAssembly().BasedOn<Profile>());
不再工作了。
如果我尝试解析配置文件(下一行),它将返回 0 个配置文件。
我不是这两个工具的专家,我不确定这是用 Windsor 初始化 AutoMapper 的最佳方法,但它一直有效。
知道为什么这不再起作用了吗?