Kris
Kris

Reputation: 524

InRequestScope is not disposing

I'm using Ninject in an ASP MVC project for database binding. In "private static IKernel CreateKernel()", I'm binding a database object like below:

kernel.Bind<IDbSession>().ToProvider<IDbSession>(new NhDbSessionProvider()).InRequestScope();

This works almost exactly as intended, but some pages on the site use AJAX calls to methods on the controller and it seems like every one of these calls opens a SQL connection and doesn't dispose of it when the controller returns. Eventually this causes the NumberOfConnections on the SQL database to exceed the max and the site throws errors that connections aren't available.

I'm pretty new to Ninject and taking over an existing project trying to make this work without doing major changes. Any ideas what I can do to get these objects to dispose? It seems like they should be automatically doing this already from what I'm reading, but maybe I just don't understand.

If I need to share more code let me know.

Upvotes: 1

Views: 94

Answers (2)

Kris
Kris

Reputation: 524

Seems someone commented out this line for whatever reason (it wasn't me). Adding it back resolves the problem.

DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));

Upvotes: 0

Owen Pauling
Owen Pauling

Reputation: 11841

Disposable instances are Disposed at end of request processing, according to the documentation.

However, the documentation for InRequestScope has a section on Ensuring Deterministic Dispose calls in a Request Processing cycle.

Specifically:

Use the Ninject.Web.Common package

Manually registering OnePerRequestModule in web.config (e.g. if you're not using a Ninject.Web.MVC* NuGet Package)

It is perhaps the case that your instances are being/will be Disposed, but just not at the time you're expecting them to be.

Upvotes: 1

Related Questions