title: StructureMap not resolving controller instances in MVC3 application
body: <p>I am in the process of integrating StructureMap into my MVC3 application. I am running into a problem where my custom ControllerFactory is throwing an exception when attempting to create a controller:</p>
  
  <blockquote>
    <p>StructureMap Exception Code:  200 Could not find an Instance named
    "contentpage" for PluginType System.Web.Mvc.IController</p>
  </blockquote>
  
  <p>Here’s what I have in the container:</p>
  
  <blockquote>
    <p>Controller (System.Web.Mvc.Controller)<br>
    Scoped as:  Transient</p>
    
    <p>AuctionCMS.Web.Controllers.HomeController, AuctionCMS.Web,
    Version=1.0.0.0, Culture=neutral, </p>
    
    <p>AuctionCMS.Web.Controllers.ContentPageController, AuctionCMS.Web,
    Version=1.0.0.0, Culture=neutral, PublicKeyToken=null     Configured
    Instance of AuctionCMS.Web.Controllers.ContentPageController,
    AuctionCMS.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null</p>
  </blockquote>
  
  <p>I did notice that the controller factory is requesting “contentpage” instead of “contentpagecontroller.” Is this correct behavior? What is going wrong?</p>
  
  <p>Here’s my code:</p>
  
  <pre><code>    private void InitStructureMap()
      {
          var container = new StructureMap.Container();
  
          DependencyResolver.SetResolver(new StructureMapContainer(container));
          ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory(container));
  
          PerformRuntimeDepdendencyConfiguration(container);
      }
  
  
  
      private void PerformRuntimeDepdendencyConfiguration(IContainer container)
      {
          container.Configure(x =&gt; x.Scan(y =&gt;
          {
              y.TheCallingAssembly();
              y.WithDefaultConventions();
              y.LookForRegistries();
              y.AddAllTypesOf&lt;Controller&gt;();
          }));
      }
  
  
  
  public class StructureMapContainer : IDependencyResolver
  {
      static IContainer _container;
  
      public StructureMapContainer(IContainer container)
      {
          _container = container;
      }
  
      public object GetService(Type serviceType)
      {
          if (serviceType.IsAbstract || serviceType.IsInterface)
          {
              return _container.TryGetInstance(serviceType);
          }
          else
          {
              return _container.GetInstance(serviceType);
          }
      }
  
      public IEnumerable&lt;object&gt; GetServices(Type serviceType)
      {
          return _container.GetAllInstances&lt;object&gt;().Where(s =&gt; s.GetType() == serviceType);
      }
  }
  
  
      public class StructureMapControllerFactory : IControllerFactory
      {
          private readonly IContainer _container;
  
          public StructureMapControllerFactory(IContainer container)
          {
              _container = container;
          }
  
          public IController CreateController(RequestContext requestContext, string controllerName)
          {
              System.Diagnostics.Debug.WriteLine(_container.WhatDoIHave()); 
  
              return _container.GetInstance&lt;IController&gt;(controllerName.ToLowerInvariant());
          }
  
          public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
          {
              return SessionStateBehavior.Default;
          }
  
          public void ReleaseController(IController controller)
          {
              return;
          }
      }
  
  public class ApplicationRegistry : Registry
  {
      public ApplicationRegistry()
      {
          ... register some types ...
      }
  }
  </code></pre>