4

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.

What I am doing:

  • 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:

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

  • I add the following binding in NinjectWebCommon RegisterServices method

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

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

    public HomeController(SomeDisposable some) { }

  • 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.

  • 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.

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.

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?

Community
  • 1
  • 1
Stilgar
  • 22,354
  • 14
  • 64
  • 101
  • 2
    This happened to me a while ago, make sure that you have all of the latest versions of Ninject's dependencies. The one that provides functionality for determining request scope has a bug that holds onto the objects much longer than needed and is fixed in a later version. Apologies for being vague, I can't remember the relevant names/versions. – Steve Lillis Dec 05 '14 at 09:18
  • 1
    Some people also reported issues with updating - whereas after updating `.InRequestScope()` didn't work anymore, at all. AFAIR Removing and reinstalling all packages in the right sequence was the key.. but don't ask me which sequence ;o – BatteryBackupUnit Dec 05 '14 at 13:11
  • I find it absurd that I can't get it to work on an empty project without changing anything from the default configuration. – Stilgar Dec 05 '14 at 13:13

1 Answers1

7

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

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

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

Frank
  • 4,461
  • 13
  • 28
  • That did work (I used update rather than install fresh package). Do you happen to know the reasoning behind installing the oldest dependencies instead of the newest non-beta ones? – Stilgar Dec 10 '14 at 12:57
  • @Stilgar There is a discussion thread about this "feature": https://nuget.codeplex.com/discussions/436712 – Frank Dec 10 '14 at 13:04
  • I had been using 3.0.0.7 from March 2012, I wonder if this exhibited the issue--I don't *think* it did because I have a RavenDB session bound to InRequestScope and it surely would have barked at me for exceeding the 30 req/session default limit. I came here looking for another issue thinking this might be it. Even if it's not, good to know about this! – kamranicus Jun 02 '15 at 00:56
  • I have this issue, and I have version 3.3.1 It, however, does dispose some of the time, and some of the time it does not. I added a console output on the constructor and a console output on the dispose, and although the dispose does get exectued some of the time, it also does not execute all the time. any thoughts? – Gerrie Pretorius May 07 '19 at 16:30