Ninject 3.0没有处理映射为InRequestScope的对象

Ric*_*d B 2 c# ninject ioc-container ninject.web.mvc asp.net-mvc-3

我正在尝试使用Ninject来管理对象的生命周期.对于我的IRepository对象,我要求实现IDisposable,并且在ConcreteRepository中,我已经实现了IDisposable来杀死我的NHibernateSession.

我的问题是我还在ConcreteRepository中放置了一个静态变量来计算ConcreteRepository的实例化和析构/处理的数量......当我运行应用程序时,我的数据库连接用完了,我的日志是显示我的应用程序永远不会释放我的连接.

我的Global.asax:

public class Global : NinjectHttpApplication
{
    protected override void OnApplicationStarted()
    {
        base.OnApplicationStarted();

        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
        ControllerBuilder.Current.DefaultNamespaces.Add("WebPortal.Controllers");

        var log4netConfigFileInfo = new System.IO.FileInfo(Server.MapPath("~/log4net.xml"));

        log4net.Config.XmlConfigurator.ConfigureAndWatch(log4netConfigFileInfo);
        log4net.ILog log = log4net.LogManager.GetLogger(typeof(Global));
        log.Info("Started...");
    }

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

    }


    protected override Ninject.IKernel CreateKernel()
    {
        var kernel = new Ninject.StandardKernel(
        new Utils.UtilsModule(),
        new Web.DataObjects.NHibernate.DataObjectsNHibernateModule(),
        new Payroll.PayrollModule(),
        new Web.DataObjects.DbModule()
        );

        kernel.Load(Assembly.GetExecutingAssembly());

        return kernel;
    }
}
Run Code Online (Sandbox Code Playgroud)

我用来测试的我的控制器模块:

public class DatabaseAreaModelModule
    : NinjectModule
{
    public override void Load()
    {
        Bind<DiscountEdit>().ToSelf().InRequestScope();
        Bind<ItemCategoryEdit>().ToSelf().InRequestScope();
        Bind<ItemEdit>().ToSelf().InRequestScope();
        Bind<ModifierEdit>().ToSelf().InRequestScope();
        Bind<ModifierSetEdit>().ToSelf().InRequestScope();
        Bind<RevenueCenterEdit>().ToSelf().InRequestScope();
        Bind<RevenueClassEdit>().ToSelf().InRequestScope();
        Bind<TaxEdit>().ToSelf().InRequestScope();
    }
}
Run Code Online (Sandbox Code Playgroud)

我的"NHibernate"Ninject模块:

public class DataObjectsNHibernateModule
    : NinjectModule
{
    public override void Load()
    {
        Bind<ISessionFactory>().ToProvider<NHibernateSessionProvider>().InSingletonScope();
        Bind<IRepository>().To<NHibernateRepository>().InRequestScope();
    }
}
Run Code Online (Sandbox Code Playgroud)

我想弄清楚的是当我要求InRequestScope()的时候,它没有被处理......任何想法?

Eri*_*sch 5

为了InRequestScope()在请求完成时获取要处理的对象,您必须加载OnePerRequestHttpModule.

老实说,我不知道为什么每个人都觉得有必要以艰难的方式做事.只需从nuget安装Ninject.MVC3,它就会为您完成所有这些工作.99.9%的"请帮助我,Ninject没有按照预期做的"问题,因为人们觉得有必要以艰难的方式做事,做错了.

为自己省点头疼.只需安装Ninject.MVC3并将绑定和/或模块复制到NinjectWebCommon.cs就可以了.

  • @blowdart - 什么是苛刻的?这是很难做到的,如果你不知道自己在做什么,你可能会犯错.Ninject.MVC3是一种简单的方法,你做对了,安装需要几秒钟. (2认同)