一切都很好,直到我用NuGet更新了castlewindsor和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的最佳方法,但它到现在为止还有效。
知道为什么这个不再工作了吗?
这是Windsor中一个已知且有记录的突破性更改(详见breakingchanges.txt
)。
简而言之,如果您将配置文件解析为Profile
,则需要将它们注册为Profile
。
Container.Register(AllTypes.FromThisAssembly().BasedOn<Profile>().WithServiceBase());
对我来说这段代码是有效的:
public class AutoMapperInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
// Register all mapper profiles
container.Register(
Classes.FromAssemblyInThisApplication(GetType().Assembly)
.BasedOn<Profile>().WithServiceBase());
// Register IConfigurationProvider with all registered profiles
container.Register(Component.For<IConfigurationProvider>().UsingFactoryMethod(kernel =>
{
return new MapperConfiguration(configuration =>
{
kernel.ResolveAll<Profile>().ToList().ForEach(configuration.AddProfile);
});
}).LifestyleSingleton());
// Register IMapper with registered IConfigurationProvider
container.Register(
Component.For<IMapper>().UsingFactoryMethod(kernel =>
new Mapper(kernel.Resolve<IConfigurationProvider>(), kernel.Resolve)));
}
}