【发布时间】:2011-04-06 14:49:52
【问题描述】:
我在使用 Ninject 将 AutoMapper 注入 ASP.NET MVC 2 应用程序时遇到问题。我使用 Jimmy Bogard 在 AutoMapper and StructureMap type Configuration 上的帖子作为指导。
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 注入到 Configuration 类型的构造函数的参数映射器中
更新
现在正在使用以下绑定:
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/
【问题讨论】:
标签: .net asp.net-mvc-2 ninject automapper bootstrapping