1

一切正常,直到我使用 NuGet 更新 CastleWinsor 和 AutoMapper 到其最新版本的引用:Castle.Windsor.3.0.0.4001AutoMapper.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 的最佳方法,但它一直有效。

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

4

2 回答 2

3

这是 Windsor 中已知且记录在案的重大变化(详情请参阅breakingchanges.txt)。

简而言之,如果您正在解析您的配置文件,Profile您需要将它们注册为Profile.

Container.Register(AllTypes.FromThisAssembly().BasedOn<Profile>().WithServiceBase());
于 2011-12-18T11:36:34.890 回答
0

For me this code worked :

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)));
        }
}
于 2020-09-17T14:24:36.190 回答