I have the Global.asax
like the code below:
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(Rou
A good place to handle this is in IControllerFactory.ReleaseController, eg
public override void ReleaseController() {
base.ReleaseController();
//Do whatever you need to clean up the IoC container here
}
In NInject this could be handled by scoping using an activation block, at the start of the request when creating the controller you can store the activation block in the HttpContext's current items, during ReleaseController you can retrieve the previously created activation block and dispose it.
You could also consider using InScope
and having the custom scope implement INotifyWhenDisposed
. After that the usage is the same as with an activation block, except now you store the scope in the HttpContext's current items.