title: Decorating ASP.NET Web API IHttpController
body: <p>I'm trying to wrap Web API controllers (<code>IHttpController</code> implementations) with decorators, but when I do this, Web API throws an exception, because somehow it is expecting the actual implementation.</p>
  
  <p>Applying decorators to controllers is a trick I successfully apply to MVC controllers and I obviously like to do the same in Web API.</p>
  
  <p>I created a custom <code>IHttpControllerActivator</code> that allows resolving decorated <code>IHttpController</code> implementations. Here's a stripped implementation:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class CrossCuttingConcernHttpControllerActivator : IHttpControllerActivator {
      private readonly Container container;
      public CrossCuttingConcernHttpControllerActivator(Container container) {
          this.container = container;
      }
  
      public IHttpController Create(HttpRequestMessage request, 
          HttpControllerDescriptor controllerDescriptor, Type controllerType)
      {
          var controller = (IHttpController)this.container.GetInstance(controllerType);
  
          // Wrap the instance in one or multiple decorators. Note that in reality, the 
          // decorator is applied by the container, but that doesn't really matter here.
          return new MyHttpControllerDecorator(controller);
      }
  }
  </code></pre>
  
  <p>My decorator looks like this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class MyHttpControllerDecorator : IHttpController {
      private readonly IHttpController decoratee;
      public MyHttpControllerDecorator(IHttpController decoratee) {
          this.decoratee = decoratee;
      }
  
      public Task&lt;HttpResponseMessage&gt; ExecuteAsync(
          HttpControllerContext controllerContext,
          CancellationToken cancellationToken)
      {
          // this decorator does not add any logic. Just the minimal amount of code to
          // reproduce the issue.
          return this.decoratee.ExecuteAsync(controllerContext, cancellationToken);
      }
  }
  </code></pre>
  
  <p>However, when I run my application and request the <code>ValuesController</code>, Web API throws me the following <code>InvalidCastException</code>:</p>
  
  <blockquote>
    <p>Unable to cast object of type 'WebApiTest.MyHttpControllerDecorator'
    to type 'WebApiTest.Controllers.ValuesController'.</p>
  </blockquote>
  
  <p>Stacktrace:</p>
  
  <pre><code>at lambda_method(Closure , Object , Object[] )
  at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.&lt;&gt;c__DisplayClass13.&lt;GetExecutor&gt;b__c(Object instance, Object[] methodParameters)
  at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
  at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.&lt;&gt;c__DisplayClass5.&lt;ExecuteAsync&gt;b__4()
  at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)
  </code></pre>
  
  <p>It's just as if Web API supplies us with the <code>IHttpController</code> abstraction but skips it and still depends on the implementation itself. This would of course be a severe violation of the Dependency Inversion principle and make the abstraction utterly useless. So I'm probably doing something wrong instead.</p>
  
  <p>What I'm I doing wrong? How can I happily decorate my API Controllers?</p>