Bootstrapper不加载我的NinjectModules

时间:2013-05-15 08:01:52

标签: c# asp.net-mvc-4 dependency-injection ninject bootstrapper

我正在尝试使用Bootstrapper with Ninject。我已经安装Ninject.MVC3来从nuget初始化我的Ninject容器,我已经创建了一个这样的测试模块:

public class TestNinjectModule : NinjectModule
{
    public override void Load()
    {
        Bind<DBEntities>().ToSelf().InRequestScope();
        Bind(typeof(IGenericRepository<>))
              .To(typeof(GenericRepository<>)).InRequestScope();
        Bind<ISystemRepository>().To<SystemRepository>().InRequestScope();
        Bind<IUnitOfWork>().To<UnitOfWork>();
    }
}

我还在Start()

中加入了Global.asax.cs来电
public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        Bootstrapper.With.Ninject().Start();
        Bootstrapper.With.AutoMapper().Start();
    }
}

但是当调用具有依赖关系的控制器时,我仍然从Ninject获得ActivationException,并且未调用我的TestNinjectModule.Load()方法。我该如何解决?

异常消息:

  

激活IUnitOfWork时出错   没有匹配的绑定可用,并且该类型不可自绑定。   激活路径:     2)将依赖项IUnitOfWork注入到类型为&gt;的构造函数的参数unitOfWork中。 TreasurySystemController     1)对SystemController的请求

     

建议:     1)确保已为IUnitOfWork定义了绑定。     2)如果在模块中定义了绑定,请确保已将模块加载到&gt;中。内核。     3)确保您没有意外创建多个内核。     4)如果使用构造函数参数,请确保参数名称与&gt;构造函数参数名称匹配。     5)如果您使用自动模块加载,请确保搜索路径和过滤器正确无误。

堆栈追踪:

  

在C:\ Projects \ Ninject \ ninject \ src \ Ninject \ KernelBase.cs中的Ninject.KernelBase.Resolve(IRequest请求):第359行      在Ninject.Planning.Targets.Target 1.GetValue(Type service, IContext parent) in c:\Projects\Ninject\ninject\src\Ninject\Planning\Targets\Target.cs:line 197 at Ninject.Planning.Targets.Target 1.ResolveWithin(IContext parent)在c:\ Projects \ Ninject \ ninject \ src \ Ninject \ Planning \ Targets \ Target.cs:第165行      at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context,ITarget target)位于c:\ Projects \ Ninject \ ninject \ src \ Ninject \ Activation \ Providers \ StandardProvider.cs:第114行      at Ninject.Activation.Providers.StandardProvider。&lt;&gt; c_ DisplayClass4.b _2(IT目标目标)位于c:\ Projects \ Ninject \ ninject \ src \ Ninject \ Activation \ Providers \ StandardProvider.cs :第96行      at System.Linq.Enumerable.WhereSelectArrayIterator 2.MoveNext() at System.Linq.Buffer 1..ctor(IEnumerable 1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable 1 source)      在Ninject.Activation.Providers.StandardProvider.Create(IContext context)中的c:\ Projects \ Ninject \ ninject \ src \ Ninject \ Activation \ Providers \ StandardProvider.cs:第96行      at Ninject.Activation.Context.Resolve()在c:\ Projects \ Ninject \ ninject \ src \ Ninject \ Activation \ Context.cs:第157行      at Ninject.KernelBase。&lt;&gt; c_ DisplayClass10.b _c(IBinding binding)在c:\ Projects \ Ninject \ ninject \ src \ Ninject \ KernelBase.cs:第386行      在System.Linq.Enumerable.WhereSelectEnumerableIterator 2.MoveNext() at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable 1来源)      在Ninject.Web.Mvc.NinjectDependencyResolver.GetService(类型serviceType)在c:\ Projects \ Ninject \ ninject.web.mvc \ mvc3 \ src \ Ninject.Web.Mvc \ NinjectDependencyResolver.cs:第56行      在System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext,Type controllerType)

1 个答案:

答案 0 :(得分:2)

我找到了解决方案。通过将NinjectWebCommon的实施更改为:

public static class NinjectWebCommon 
{
    private static readonly Bootstrapper bootstrapper = new Bootstrapper();
    public static IKernel Kernel { get; private set; } // Expose the kernel through a property

    /// <summary>
    /// Starts the application
    /// </summary>
    public static void Start() 
    {
        DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
        DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
        bootstrapper.Initialize(CreateKernel);
    }

    /// <summary>
    /// Stops the application.
    /// </summary>
    public static void Stop()
    {
        bootstrapper.ShutDown();
    }

    /// <summary>
    /// Creates the kernel that will manage your application.
    /// </summary>
    /// <returns>The created kernel.</returns>
    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

        return Kernel = kernel;
    }
}

然后在Global.asax.cs

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        Bootstrapper.With.AutoMapper().Start();
        Bootstrapper.With.Ninject()
            .WithContainer(NinjectWebCommon.Kernel) // Use the kernel inside NinjectWebCommon instead of creating a new one
            .Start();
    }
}

然后,从NinjectModule派生的所有类都将调用Load()方法。