I'm using Ninject for DI and the Ninject.MVC3 extension within an MVC4 app, specifically version 3.0.0.6.
Reading the documentation on Ninject's wiki, it appears that the lifetime of objects created with InRequestScope
are actually controlled by ASP.NET and to properly clean up one should register the OnePerRequest
HttpModule.
However it looks like this doco is out of date as type="Ninject.OnePerRequestModule"/>
cannot be found.
On the other hand I read some bits recently that seemed to suggest that NinjectHttpApplication
already deals with OnePerRequest
.
My question is whether Ninject.Web.MVC 3.0 already deals with this issue or if I still have to manually add the HttpModule to my 'web.config'.
I have added the below to my config and everything appears to be working, though I have no idea at all how to test for when objects are destroyed:
<system.web>
<httpModules>
<add name="OnePerRequestModule" type="Ninject.Web.Common.OnePerRequestHttpModule"/>
</httpModules>
...
Do I need to manually add the OnePerRequest
HttpModule or does Ninject.Web.MVC handle this for me?
Exec summary: No, you don't need to do anything in the web.config
.
You could manually sanity check by:
IDisposable
.InRequestScope()
void Dispose()
HttpRequest
If you hit the Dispose
, it woz Ninject that shot the bear (and you'll have a call stack to prove it).
What wires it it? The Ninject.Web.Common
NuGet package adds:
public static class NinjectWebCommon
{
public static void Start()
{
...
DynamicModuleUtility.RegisterModule( typeof( OnePerRequestHttpModule ) );
DynamicModuleUtility
is from Microsoft.Web.Infrastructure
(and the call to the Start
method is driven by:
[assembly: WebActivator.PreApplicationStartMethod( typeof( App_Start.NinjectWebCommon ), "Start" )]
(Which comes from the WebActivator
NuGet package)
Speaking pedantically, the docco sayssaid:
To get more deterministic behavior, you can do any of the following:
...
Use the Ninject.Web.Common extensions that register the OnePerRequestModule for you.
...
You can register
(my emphasis). I'll make it more explicit...
BTW at the start of the exercise of answering this question, I had no special knowledge, just the source to Ninject
Ninject.Web.Common
and one of my apps.