IoC and dataContext disposing in asp.net mvc 2 application

后端 未结 3 694
北荒
北荒 2021-01-07 00:34

I have the Global.asax like the code below:

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(Rou         


        
3条回答
  •  既然无缘
    2021-01-07 01:06

    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.

提交回复
热议问题