c# - 向 CaSTLe Windsor 注册 AutoMapper 5.1.1

标签 c# castle-windsor automapper-5

我正在尝试向 CaSTLeWindsor 注册 AutoMapper 5.1.1,但我不知道在哪里正确调用 Mapper.Initialize()。

AutoMapper 配置文件:

namespace AutoMapper_DI.Mappings
{
    public class WebMappingProfile : Profile
    {        
        public WebMappingProfile()
        {
          CreateMap<Person, PersonDTO>();            
        }
    }
}

温莎城堡注册:

class MainInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {            
        container.AddFacility<TypedFactoryFacility>();

        container.Register(Component.For<IMapper>().UsingFactoryMethod(x =>
        {
            return new MapperConfiguration(c =>
            {
                c.AddProfile<WebMappingProfile>();
            }).CreateMapper();
        }));

        container.Register(Component.For<MainClass>());
    }
}

然后当我使用 _mapper 时,我得到了 Mapper not initialized 异常:

class MainClass
{
    private readonly IMapper _mapper;

    public MainClass(IMapper mapper)
    {
        _mapper = mapper;
    }

    public void Start()
    {            
        Person p = new Person
        {
            Name = "Somebody",
            Surname = "Nobody",
            Birth = new DateTime(1984, 06, 18)
        };            
        var personDTO = Mapper.Map<Person, PersonDTO>(p);

    }

}

感谢您的任何建议。

最佳答案

所以,上面的代码是有效的。问题是,我是个白痴。因为我不应该调用 Mapper.Map,而是调用 _mapper.Map。

关于c# - 向 CaSTLe Windsor 注册 AutoMapper 5.1.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39662607/

相关文章:

c# - 如何将字典参数传递给 web-api 方法?

c# - 放置 WindsorContainer 并避免循环项目引用的最佳位置在哪里?

c# - CaSTLe.Windsor - 为什么名称需要与界面相似?

c# - 从双映射

c# - 为什么我的 InnerException 没有发送给客户端?

c# - 判断集合是否相等(集合由集合组成)

c# - 将 textwriter 分配给 memory writer

caSTLe-windsor - MVC5 注入(inject)对带有城堡温莎的 View 的依赖

c# - 如果 Automapper 5.2 在 Base DTO 映射中配置,则忽略 ExplicitExpansion