我在使用Ninject将AutoMapper注入到ASP.NET MVC 2应用程序中遇到了问题。我使用了Jimmy Bogard在AutoMapper和StructureMap类型配置的帖子作为指南。
public class AutoMapperModule : NinjectModule
{
public override void Load()
{
Bind<ITypeMapFactory>().To<TypeMapFactory>();
Bind<Configuration>().ToSelf().InSingletonScope().WithConstructorArgument("mapper", MapperRegistry.AllMappers);
Bind<IConfiguration>().To<Configuration>();
Bind<IConfigurationProvider>().To<Configuration>();
Bind<IMappingEngine>().To<MappingEngine>();
}
}
Ninject在解析Configuration
时会抛出异常。
激活IObjectMapper出错 没有可用的匹配绑定,且该类型不能自我绑定。 激活路径:
3) 在类型Configuration的构造函数的mappers参数中注入依赖项IObjectMapper
更新
现在可以使用以下绑定工作:
Bind<ITypeMapFactory>().To<TypeMapFactory>();
Bind<Configuration>().ToConstant(new Configuration(Kernel.Get<ITypeMapFactory>(), MapperRegistry.AllMappers())).InSingletonScope();
Bind<IConfiguration>().ToMethod(c => c.Kernel.Get<Configuration>());
Bind<IConfigurationProvider>().ToMethod(c => c.Kernel.Get<Configuration>());
Bind<IMappingEngine>().To<MappingEngine>();
我在GitHub上发布了这个模块。AutoMapper.Ninject。更多信息请参见我的博客:http://binaryspeakeasy.com/2010/09/automapper-ninject/