A few days ago, I had an issue with ASP.Net threading. I wanted to have a singleton object per web request. I actually need this for my unit of work. I wanted to instantiate
Thanks for your contribution,
But the question proposed implementation has two drawbacks, one of which is a serious bug as already stated by Steven Robbins in his answer and Micah Zoltu in a comment.
Currently, Unity.Mvc Nuget package supplies a PerRequestLifetimeManager for doing the work. Do not forget to register its associated UnityPerRequestHttpModule
in bootstrapping code otherwise dependencies releasing will not be handled either.
Using bootstrapping
DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
or in web.config system.webServer/modules
It appears its current implementation is also suitable for web forms. And it does not even depend on MVC. Unfortunately, its assembly does, because of some other classes it contains.
Beware, in case you use some custom http module using your resolved dependencies, they may be already disposed in the module EndRequest
. It depends on module execution order.