title: ASP.NET MVC and Unity 1.2 Container question
body: <p>I am trying to use the Unity container to make it easier to unit test my controllers. My controller uses a constructor that accepts an interface to a Repository. In the global.asax file, I instantiate a UnityContainerFactory and register it with the MVC framework and then register the repository and its implementation. I added the [Dependency] attribute to the controller’s CTOR Repository parameter. This all seems to work OK, except that occasionally the factory’s GetControllerInstance(Type controllerType) is called more than once and is passed a null argument as the controllerType. </p>
  
  <p>The first call to the factory is aways correct and the controllerType “ProductsController” is passed-in as an argument. But sometimes, the factory is called a couple more times after the view has been displayed with a null value for the controller and I am not sure why. When the correct value of the controller type is passed that “Call Stack” makes sense to me, but when a null is passed, I am not sure why or who is making the call. Any ideas?</p>
  
  <p>The code and call stacks for the example are shown below.</p>
  
  <p>Call Stack when is works</p>
  
  <p>Test.DLL!Test.UnityHelpers.UnityControllerFactory.GetControllerInstance(System.Type controllerType = {Name = "ProductsController" FullName = "Test.Controllers.ProductsController"}) Line 23    C#
  Test.DLL!Test._Default.Page_Load(object sender = {ASP.default_aspx}, System.EventArgs e = {System.EventArgs}) Line 18 + 0x1a bytes  C#</p>
  
  <p>Call Stack when NULL is passed at the controllerType</p>
  
  <p>Test.DLL!Test.UnityHelpers.UnityControllerFactory.GetControllerInstance(System.Type controllerType = null) Line 27  C#</p>
  
  <p>First I created a UnityControllerFactory </p>
  
  <pre><code>public class UnityControllerFactory : DefaultControllerFactory
  {
      UnityContainer container;
  
      public UnityControllerFactory(UnityContainer container)
      {
          this.container = container;
      }
  
      protected override IController GetControllerInstance(Type controllerType)
      {
          if (controllerType != null)
          {
              return container.Resolve(controllerType) as IController;
          }
          else
          {
              return null; // I never expect to get here, but I do sometimes, the callstack does not show the caller
          }
      }
  }
  </code></pre>
  
  <p>Next, I added the following code the global.asax file to instantiate the container factory</p>
  
  <pre><code>    protected void Application_Start()
      {
          RegisterRoutes(RouteTable.Routes);
  
          // Create Unity Container if needed
          if (_container == null)
          {
              _container = new UnityContainer();
          }
  
          // Instantiate a new factory
          IControllerFactory unityControllerFactory = new UnityControllerFactory(_container);
  
          // Register it with the MVC framework
          ControllerBuilder.Current.SetControllerFactory(unityControllerFactory);
  
          // Register the SqlProductRepository
          _container.RegisterType&lt;IProductsRepository, SqlProductRepository&gt;
              (new ContainerControlledLifetimeManager());
      }
  </code></pre>
  
  <p>The app has one controller</p>
  
  <pre><code>public class ProductsController : Controller
  {
      public IProductsRepository productsRepository;
  
      public ProductsController([Dependency]IProductsRepository productsRepository)
      {
         this.productsRepository = productsRepository;
      }
  }
  </code></pre>