Castle Windsor在更新后无法解析AutoMapper配置文件

时间:2011-12-18 11:15:28

标签: castle-windsor ioc-container automapper automapper-2

一切正常,直到我使用NuGet将CastleWinsor和AutoMapper的参考资料更新为最新版本:Castle.Windsor.3.0.0.4001AutoMapper.2.0.0

我在与AutoMapperInstaller : IWindsorInstaller相同的程序集中有一个AutoMapper配置文件列表。它们位于不同的名称空间中,但这不重要,对吗?

以下是个人资料示例:

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的最佳方法,但它一直有效。

知道为什么这不再适用了吗?

2 个答案:

答案 0 :(得分:2)

这是Windsor中已知且有记录的重大变化(有关详细信息,请参阅breakingchanges.txt。)

简而言之,如果您要将个人资料解析为Profile,则需要将其注册为Profile

Container.Register(AllTypes.FromThisAssembly().BasedOn<Profile>().WithServiceBase());

答案 1 :(得分:0)

对我来说,这段代码有效:

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)));
        }
}