Asp.net mvc Ninject mvc InRequestScope

Asp.net mvc Ninject mvc InRequestScope,asp.net-mvc,ninject.web.mvc,Asp.net Mvc,Ninject.web.mvc,我将NuGet的ninject与MVC4应用程序一起使用 public class MvcApplication : System.Web.HttpApplication 我知道我可以使用ninject类作为bas类,但我没有 在我的global.asax.cs中 ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory()); 在哪里 public class NinjectControllerF

我将NuGet的ninject与MVC4应用程序一起使用

public class MvcApplication : System.Web.HttpApplication
我知道我可以使用ninject类作为bas类,但我没有

在我的global.asax.cs中

ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
在哪里

public class NinjectControllerFactory : DefaultControllerFactory  {
    private IKernel _ninjectKernel;

    public NinjectControllerFactory() {
        _ninjectKernel = new StandardKernel();
        AddBindings();
    }

    public T GetInstance<T>() {
        return _ninjectKernel.Get<T>();
    }

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) {
        return controllerType == null
            ? null
            : (IController)_ninjectKernel.Get(controllerType);
    }

    private void AddBindings() {
        _ninjectKernel.Bind<IVIPRepository>().To<VIPRepository>().InRequestScope().
            WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings["VIPFullContext"].ConnectionString);
        _ninjectKernel.Bind<IPrezentationProvider>().To<HardPrezProvider.HardPrezentationProvider>().InRequestScope(); ;
    }
}
公共类NinjectControllerFactory:DefaultControllerFactory{
私有IKernel Ninject内核;
公共控制器工厂(){
_ninjectKernel=新的标准内核();
AddBindings();
}
公共T GetInstance(){
返回_ninjectKernel.Get();
}
受保护的重写IController GetControllerInstance(RequestContext RequestContext,类型controllerType){
返回控制器类型==null
无效的
:(IController)\u ninjectKernel.Get(controllerType);
}
私有void AddBindings(){
_ninjectKernel.Bind()到()InRequestScope()。
使用ConstructorArgument(“connectionString”,ConfigurationManager.connectionString[“VIPFullContext”].connectionString);
_ninjectKernel.Bind().To().InRequestScope();
}
}
在我的web.config中,我加载模块(并检查它是否加载良好)


我的控制器源于
BaseController(IVIPRepository repo)
。应用程序运行良好。。。但似乎存储库(以及相关的上下文)从未被释放(VIPRepository的dispose方法中的断点)

我还通过删除
NinjectWebCommon.cs
来清除应用程序,因为我认为我已经正确设置了Ninject。。。。似乎不是

我做错了什么


提前感谢

我建议您查看这篇关于MVC 3/4版Ninject nuget软件包的优秀且易于阅读的帖子:

有了nuget软件包,安装看起来非常简单,可以在30秒内完成

<system.webServer>
    <modules>
        <add name="OnePerRequestHttpModule" type="Ninject.Web.Common.OnePerRequestHttpModule"/>
    </modules>
</system.webServer>