无法获得Ninject处置对象请求范围 [英] Can't get Ninject to dispose object in Request Scope

查看:132
本文介绍了无法获得Ninject处置对象请求范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法得到Ninject处置在请求范围的对象与网页API ASP.NET MVC应用程序,无论我做什么。

I can't seem to get Ninject to dispose objects in request scope in an ASP.NET MVC application with web API no matter what I do.

我在做什么:


  • 创建与Visual Studio 2013年一个新的ASP.NET Web应用程序我选择MVC模板,并添加一个Web的API(它也有ASP.NET身份在包默认情况下)

  • 我安装通过的NuGet Ninject.MVC5包(安装包Ninject.MVC5)

  • 我添加下面的类,以我的应用程序:

  • Create a new ASP.NET Web Application with Visual Studio 2013. I select the MVC template and add a Web API to it (it also has ASP.NET Identity in the package by default)
  • I install Ninject.MVC5 package via nuget (install-package Ninject.MVC5)
  • I add a the following class to my application:

公共类SomeDisposable:IDisposable接口
{
    公共无效的Dispose()
    {
        System.Diagnostics.Debug.WriteLine(测试);
    }
}

public class SomeDisposable : IDisposable { public void Dispose() { System.Diagnostics.Debug.WriteLine("test"); } }

我加入NinjectWebCommon RegisterServices方法如下结合

I add the following binding in NinjectWebCommon RegisterServices method

kernel.Bind()ToSelf()InRequestScope();

kernel.Bind().ToSelf().InRequestScope();

我型SomeDisposable的对象添加到主控制器

I add an object of the type SomeDisposable to the Home controller

公开的HomeController(SomeDisposable一些)
{
}

public HomeController(SomeDisposable some) { }

我运行应用程序并放置一个断点在HomeController的构造函数和类SomeDisposable的Dispose方法。控制器接收对象(psumably从Ninject $ P $),页面加载,但的Dispose方法不会被调用

I run the application and place a breakpoint in the HomeController constructor and the Dispose method of the SomeDisposable class. The controller receives an object (presumably from Ninject), the page loads but the Dispose method is never called.

在这一点上的东西都已经突破,但我想补充一个Web API控制器,​​安装Ninject.WebApi打包并与控制器的WebAPI重复这个实验,我也得到了同样的结果。

At this point things are already broken but I add a Web API controller, install Ninject.WebApi packaged and repeat the experiment with the WebAPI controller and I get the same result.

我读了一堆问题,包括这一个 - <一个href=\"http://stackoverflow.com/questions/5145775/ninject-doesnt-call-dispose-on-objects-when-out-of-scope\">Ninject不调用Dispose对象时出范围​​和Ninject文档 https://github.com/ninject/Ninject.Web.Common/wiki/InRequestScope 以及它们都表明,由于这样的事实,Ninject.Web.Common包括用于OnePerRequestHttpModule登记(是它的存在)处置应该只是工作,但事实并非如此。我也试着在web.config中添加PerRequest模块,并得到了一个错误说我不能在集成模式本节。

I have read a bunch of questions including this one - Ninject doesn't call Dispose on objects when out of scope and the Ninject documentation https://github.com/ninject/Ninject.Web.Common/wiki/InRequestScope and they both indicate that due to the fact that Ninject.Web.Common includes a registration for the OnePerRequestHttpModule (yes it is there) the disposal should just work but it doesn't. I also tried adding the PerRequest module in the web.config and got an error saying that I can't have this section in integrated mode.

在这一点上,我失去了。我怀疑不是集成模式或OWIN有一些做的,但我不知道如何调试它,或者做些什么来解决它。有什么建议?

At this point I am lost. I suspect either integrated mode or OWIN have something to do with this but I have no idea how to debug it or what to do to fix it. Any suggestions?

推荐答案

没有被设置的目的是3.2.2版本之前,在 Ninject.Web.Common 的一个问题,因为这里讨论: InRequestScope未能处置对象

The object not being disposed was a problem in Ninject.Web.Common before version 3.2.2, as discussed here: InRequestScope is failing to dispose objects

当你已经通过的NuGet安装Ninject,你可能安装了最古老的支持依赖。这可以通过使用来避免:

As you have been installing Ninject via nuget, you probably installed the oldest supported dependencies. This can be avoided by using:

Install-Packages Ninject.MVC5 -DependencyVersion Highest

请确认您正在使用的所有Ninject软件包的最新版本。

Please verify that you are using the current version of all Ninject packages.

这篇关于无法获得Ninject处置对象请求范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆