我在使用Ninject将AutoMapper注入ASP.NET MVC 2应用程序时遇到问题。我用吉米博加德的文章AutoMapper and StructureMap type Configuration作为指导。使用Ninject注入AutoMapper依赖关系
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)依赖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/
请参阅http://stackoverflow.com/a/1810728/11635 – 2012-06-12 06:21:30