136

Very simple question: Are controllers in ASP.NET created for every HTTP request, or are they created at application startup and reused throughout requests?

Will the controller be created only for a particular HTTP request?

If my previous assumptions are correct, can I depend on it? I want to create database context (Entity Framework) that will live only for one request. If I create it as a property initialized in controller's constructor, is it granted that new instance of context will be created on for every request?

4
  • 16
    Put a breakpoint in your constructor and see what you can find out...
    – Greg B
    Mar 24, 2011 at 21:50
  • 12
    @Greg B: great idea except it will not tell me if it behaves like that always - if circumstances changes and some controller will change its behavior I have bug that might be really hard to find...
    – Rasto
    Mar 24, 2011 at 21:54
  • 4
    @Todd Smith please some link or at least full name. Tree letters IoC are hard to google. Thank you.
    – Rasto
    Mar 24, 2011 at 22:12
  • 2
    @drasto IoC = Inversion of control en.wikipedia.org/wiki/Inversion_of_control
    – Bala R
    Mar 24, 2011 at 22:23

4 Answers 4

115

A Controller is created for every request by the ControllerFactory (which by default is the DefaultControllerFactory).

http://msdn.microsoft.com/en-us/library/system.web.mvc.defaultcontrollerfactory.aspx

Note that the Html.Action Html Helper will create another controller.

The short version is that ControllerActivator.Create is called (for every request) to create a Controller (which inits a new Controller either through the DependencyResolver or through the Activator if no Resolver has been set up):

public IController Create(RequestContext requestContext, Type controllerType) 
{
    try 
    {
        return (IController)(_resolverThunk().GetService(controllerType) ?? Activator.CreateInstance(controllerType));
    }

The longer version is this (Here's the code from the source from the MvcHandler):

protected internal virtual void ProcessRequest(HttpContextBase httpContext)
{
    SecurityUtil.ProcessInApplicationTrust(() =>
    {
        IController controller;
        IControllerFactory factory;
        ProcessRequestInit(httpContext, out controller, out factory);

        try
        {
            controller.Execute(RequestContext);
        }
        finally
        {
            factory.ReleaseController(controller);
        }
    });
}

private void ProcessRequestInit(HttpContextBase httpContext, out IController controller, out IControllerFactory factory)
{
    // non-relevant code
    // Instantiate the controller and call Execute
    factory = ControllerBuilder.GetControllerFactory();
    controller = factory.CreateController(RequestContext, controllerName);
    if (controller == null)
    {
        throw new InvalidOperationException(
            String.Format(
                CultureInfo.CurrentCulture,
                MvcResources.ControllerBuilder_FactoryReturnedNull,
                factory.GetType(),
                controllerName));
    }
}

Here's the Controller factory code:

public virtual IController CreateController(RequestContext requestContext, string controllerName) 
{
    Type controllerType = GetControllerType(requestContext, controllerName);
    IController controller = GetControllerInstance(requestContext, controllerType);
    return controller;
}

Which basically calls this:

protected internal virtual IController GetControllerInstance(RequestContext requestContext, Type controllerType) 
{
    return ControllerActivator.Create(requestContext, controllerType);
}

Which calls this method in the ControllerActivator (This code tries to ask the DependencyResolver for an instance, or just uses the Activator class):

public IController Create(RequestContext requestContext, Type controllerType) 
{
    try 
    {
        return (IController)(_resolverThunk().GetService(controllerType) ?? Activator.CreateInstance(controllerType));
    }

This might fall under too much information... But I wanted to show that you really DO get a new controller for EVERY request.

1
37

I created an empty constructor for a controller and put a break point in the constructor. It got hit every time there was a new request. So I think it's created for every request.

2
  • 3
    +1 I hope you are right but I would like some better approved knowledge than just "in all cases that I've tried it worked". If it sometimes does not work like that from some reason it mean a bug.
    – Rasto
    Mar 24, 2011 at 21:58
  • 7
    @drasto: No need to worry. Controller gets instantiated for every request. Some memory does get reused though but you shouldn't worry about controller state (if yours has one). It will be initialized as expected. But there can be a situation where more than one controller will get instantiated. And that's when views call controller actions (ie. Html.RenderAction("action", "controller");) Mar 24, 2011 at 23:24
3

The controller will be created when any Action in a specific Controller is performed.

I have a project where all of my Controllers inherit from an ApplicationController and every time that an action is performed, the breakpoint is hit inside of the ApplicationController - regardless of its "current" Controller.

I initialize my agent (which works as my context) whenever my controller is created like such:

    public IWidgetAgent widgetAgent { get; set; }

    public WidgetController()
    {
        if (widgetAgent == null)
        {
            widgetAgent = new WidgetAgent();
        }

    }

This is obviously not what you need - as you mentioned that you only wanted a single instance each time it was called. But it is a good place to check what is going on each time and to ensure that another instance of your context does not currently exist.

Hope this helps.

2

Controllers are created for every request. The magic happens in the routing in the gobal.aspx. The mapping paths direct MVC to which controller to create and action on the controller to call, and parameters to pass to them.

http://www.asp.net/mvc/tutorials/asp-net-mvc-routing-overview-vb

1
  • citation needed - cannot find supporting information in linked document. Thank you
    – Rasto
    Mar 24, 2011 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.