ASP.NET MVC Controller Lifecycle

It's my understanding that the constructor for a controller is not called during each web request. Assuming this is true, what is the lifecycle of a controller? Is is "constructed" upon app start, then cached and invoked with the requestcontext injected into it with each web request?

Just to be clear, I'm not asking how to emulate constructor behavior, I use the OnActionExecuting event to initiate things I would normally do in a constructor. Also, I do use constructors on controllers for unit and system testing.

Thanks!


Solution 1:

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.

Solution 2:

I'm afraid, your understanding is wrong. A controller (which should be a very thin and lightweight class and must not have any session-outliving state) is actually constructed on the fly for each and every web request. How else could a controller instance be specific to a certain view?

So there is no such thing as a "lifecycle" (other than that of the request)...