ASP.NET MVC Controller Lifecycle
If you use the default controller factory a new instance will be constructed for each request and that's the way it should be. Controllers shouldn't be shared among different requests. You could though write a custom factory that manages the lifetime of the controllers.
What is the lifetime of a ASP.NET MVC Controller?
Stephen Walther has a great article on the life-cycle of a request being handled by the MVC Framework.
Here's a extract from the top of his article, it goes on to explain each step in detail:
Overview of the Lifecycle Steps
There are five main steps that happen when you make a request from an ASP.NET MVC website:
1. The RouteTable is Created
This first step happens only once when an ASP.NET application first starts. The RouteTable maps URLs to handlers.
2. The UrlRoutingModule Intercepts the Request
This second step happens whenever you make a request. The UrlRoutingModule intercepts every request and creates and executes the right handler.
3. The MvcHandler Executes
The MvcHandler creates a controller, passes the controller a ControllerContext, and executes the controller.
4. The Controller Executes
The controller determines which controller method to execute, builds a list of parameters, and executes the method.
5. The RenderView Method is Called
Typically, a controller method calls RenderView() to render content back to the browser. The Controller.RenderView() method delegates its work to a particular ViewEngine
When in the ASP.NET MVC 3 Controller lifecycle does the controllerContext get constructed?
When in the ASP.NET MVC 3 Controller lifecycle does the ControllerContext get constructed?
This happens inside the Initialize
method. Never access any HttpContext bound object in a controller constructor.
ASP.NET MVC Controller Factory life cycle
CreateController
gets called.
It first calls GetControllerType
to figure out type of the controller, then passes this type to GetControllerInstance
.
Because it is easier (and often enough) to (only) override GetControllerInstance
(so that the logic behind selecting the type remains the same), you'd often see this happening.
WebApi Controller Lifecycle
The User is only assigned well after the controller is created. There are no events on ApiControllers that you can handle to do that. The most common way to this is to extract a method that retrieves the extra information and call it in every action
MVC Request Lifecycle - When / If object disposal occur?
A new instance to the controller object is created for every request. This means that the reference to this controller is lost once the action is performed. There on it is left to the garbage collector to dispose and close all the resource intense objects.
So, the best practice is always to close and dispose all the heavy duty objects in the action method itself.
I understand that the view would require live connections when binding EF objects. Again the best practice here is to enumerate them before binding to the view.
The answer to your question is - The garbage collector will be responsible for disposing or closing the connection that are left open in action method - And this is a formula for disaster.
EDIT
Note: Controller class implements IDisposable, so it should be disposed after the request is served. However when I checked the source code of DefaultControllerFactory, I couldnt figure any using scope or explicit dispose call.
reference
http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/5d4159c85ff6#src/System.Web.Mvc/DefaultControllerFactory.cs
ASP.NET MVC Lifecycle - Do Some Work Once Per Page Request
You can check in the Initialize
if the action is a Child Action.
ControllerContext.IsChildAction
Related Topics
How to Hide Public Methods from Intellisense
How to Convert Datetime in Specific Timezone
"Padding Is Invalid and Cannot Be Removed" Using Aesmanaged
Opening Process and Changing Window Position
How to Sort a List<T> by Multiple T.Attributes
How to Generate and Auto Increment Id with Entity Framework
How to Implement One "Catch'Em All" Exception Handler with Resume
Why Are Constructors Not Inherited in C#
Are .Net Switch Statements Hashed or Indexed
Child Actions Are Not Allowed to Perform Redirect Actions, After Setting the Site on Https
Return All Enumerables with Yield Return at Once; Without Looping Through
How to Catch Exceptions from a Threadpool.Queueuserworkitem
Why Is Double.Nan Not Equal to Itself
Sortable Jqgrid Using Linq to MySQL (Dblinq) and Dynamic Linq - Orderby Doesn't Work
"Nested Foreach" VS "Lambda/Linq Query" Performance(Linq-To-Objects)