Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP MVC: When is IController Dispose() called?

I'm going through a big refactoring / speed tweaking of one of my larger MVC apps. It has been deployed to production for a few months now, and I was starting to get timeouts waiting for connections in the connection pool. I have tracked the issue down to the connections not getting disposed properly.

In light of that, I have since made this change to my base controller:

public class MyBaseController : Controller
{
    private ConfigurationManager configManager;  // Manages the data context.

    public MyBaseController()
    {
         configManager = new ConfigurationManager();
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (this.configManager != null)
            {
                this.configManager.Dispose();
                this.configManager = null;
            }
        }

        base.Dispose(disposing);
    }
}

Now, I have two questions:

  1. Am I introducing a race condition? Since the configManager manages the DataContext that exposes IQueryable<> parameters to the views, I need to make sure that Dispose() will not be called on the controller before the view finishes rendering.
  2. Does the MVC framework call Dispose() on the Controller before or after the view is rendered? Or, does the MVC framework leave that up to the GarbageCollector?
like image 323
John Gietzen Avatar asked Sep 04 '09 15:09

John Gietzen


People also ask

What is Dispose method in ASP NET MVC?

The Dispose method leaves the Controller instance in an unusable state. After you call Dispose, you must release all references to the Controller instance so that the garbage collector can reclaim the memory that the Controller instance was occupying.

How do you dispose of an object in .NET core?

Another approach to dispose IDisposable objects automatically is by using the built-in IoC (inversion of control) container in ASP.NET Core. You can take advantage of either Transient, Scoped, or Singleton instances to created services and add them to the built-in IoC container.

What is mvc6?

MVC 6 is a part of ASP.NET 5 that has been designed for cloud-optimized applications. The runtime automatically picks the correct version of the library when our MVC application is deployed to the cloud. The Core CLR is also supposed to be tuned with a high resource-efficient optimization.


2 Answers

Dispose is called after the view is rendered, always.

The view is rendered in the call to ActionResult.ExecuteResult. That's called (indirectly) by ControllerActionInvoker.InvokeAction, which is in turn called by ControllerBase.ExecuteCore.

Since the controller is in the call stack when the view is rendered, it cannot be disposed then.

like image 175
Craig Stuntz Avatar answered Oct 10 '22 00:10

Craig Stuntz


Just to expand on Craig Stuntz's Answer:

The ControllerFactory handles when a Controller is disposed. When implementing the IControllerFactory interface, one of the methods that needs to be implemented is ReleaseController.

I am not sure what ControllerFactory you are using, whether you rolled your own, but in Reflector looking at the DefaultControllerFactory, the ReleaseController method is implemented like so:

public virtual void ReleaseController(IController controller)
{
    IDisposable disposable = controller as IDisposable;
    if (disposable != null)
    {
        disposable.Dispose();
    }
}

An IController reference is passed in, if that controller implements IDisposable, then that controllers Dispose method is called. So, if you have anything you need disposing after the request is finished, which is after the view is rendered. Inherit off of IDisposable and put your logic in the Dispose method to release any resources.

The ReleaseController method is called by the System.Web.Mvc.MvcHandler which handles the request and it implements IHttpHandler. The ProcessRequest takes the HttpContext given to it and starts the process of finding the controller to handle the request, by calling into the implemented ControllerFactory. If you look in the ProcessRequest method you will see the finally block which calls the ControllerFactory's ReleaseController. This is only called when the Controller has returned a ViewResult.

like image 25
Dale Ragan Avatar answered Oct 10 '22 02:10

Dale Ragan