title: SimpleInjector with Sitecore and WebApi controller does not have a default constructor
body: <p>We have integrated SimpleInjector (<code>4.4.x</code>) in our <code>Sitecore 8.2</code> <a href="https://helix.sitecore.net/" rel="nofollow noreferrer">Helix</a> project.</p>
  
  <p>We have a Dependency Injection project in our <a href="https://helix.sitecore.net/principles/architecture-principles/layers.html#foundation-layer" rel="nofollow noreferrer">Foundation Layer</a>, which consists of the following pipeline:</p>
  
  <pre><code>public void Process(PipelineArgs args)
  {
      var container = new Container();
      container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
  
      // register app dependencies (omitted for readability)
  
      // get assemblies of our application
      container.RegisterMvcControllers(assemblies);
      container.RegisterWebApiControllers(GlobalConfiguration.Configuration,assemblies);
  
      container.Verify();
  
      DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
      GlobalConfiguration.Configuration.DependencyResolver =
          new SimpleInjectorWebApiDependencyResolver(container);
  }
  </code></pre>
  
  <p>Also as described in <a href="https://ctor.io/one-way-to-implement-dependency-injection-for-sitecore-habitat/" rel="nofollow noreferrer">this post</a>, the pipeline processor is implemented in the Sitecore <code>initialize</code> pipeline:</p>
  
  <pre><code>&lt;configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"&gt;
    &lt;sitecore&gt;
      &lt;pipelines&gt;
        &lt;initializeDependencyInjection/&gt;
        &lt;initialize&gt;
          &lt;processor type="Company.Foundation.Example.DependencyInjectionProcessor, Company.Foundation.Example"
                     patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeControllerFactory, Sitecore.Mvc']" /&gt;
        &lt;/initialize&gt;
      &lt;/pipelines&gt;
    &lt;/sitecore&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>As you can see, both <code>ASP.NET MVC</code> and <code>WebApi</code> are used (<code>.NET 4.6</code>). 
  Our solution consists only of MVC controllers. What we are trying to achieve is introducing <code>WebApi</code> in our solution. When adding the following controller, all works well:</p>
  
  <pre><code>public class HelloController : ApiController
  {
      [HttpGet, Route("api/hello")]
      public IHttpActionResult Get()
      {
          return Ok("Hello World!");
      }
  }
  </code></pre>
  
  <p>But when I add a dependency (and also register) e.g.:</p>
  
  <pre><code>public interface IFoo
  {
      string Hello { get; }
  }
  
  public class Foo : IFoo
  {
      public string Hello =&gt; "Hello World!";
  }
  
  public class HelloController : ApiController
  {
      private readonly IFoo _foo;
  
      public HelloController(IFoo foo)
      {
          _foo = foo;
      }
  
      [HttpGet, Route("api/hello")]
      public IHttpActionResult Get()
      {
          return Ok(_foo.Hello);
      }
  }
  </code></pre>
  
  <p>I'm getting the following exception message in runtime when doing the HTTP request:</p>
  
  <blockquote>
    <p>System.InvalidOperationException: An error occurred when trying to create a controller of type 'HelloController'. Make sure that the controller has a parameterless public constructor.</p>
  </blockquote>
  
  <p>Stack trace:</p>
  
  <pre><code>at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
  at System.Web.Http.Dispatcher.HttpControllerDispatcher.&lt;SendAsync&gt;d__15.MoveNext()
  </code></pre>
  
  <p>InnerException:</p>
  
  <blockquote>
    <p>System.ArgumentException: Type 'Company.Feature.Example.HelloController' does not have a default constructor</p>
  </blockquote>
  
  <pre><code>at System.Linq.Expressions.Expression.New(Type type)
  at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)
  at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1&amp; activator)
  at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
  </code></pre>
  
  <p>What strikes me weird is that <code>container.Verify()</code> does not throw any exceptions or warnings. When debugging, I can see that the <code>HelloController</code> is registered in the <code>Root Registrations</code> of the <code>container</code>.</p>
  
  <p>Also the binding redirects for <code>WebApi</code> are set in the <code>web.config</code> of the root project:</p>
  
  <pre><code>&lt;dependentAssembly&gt;
    &lt;assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" xmlns="urn:schemas-microsoft-com:asm.v1" /&gt;
    &lt;bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" xmlns="urn:schemas-microsoft-com:asm.v1" /&gt;
  &lt;/dependentAssembly&gt;
  </code></pre>