title: Using Castle Dynamic Proxy with WebApi Controllers
body: <p>I am trying to implement AOP logging using castle's dynamic proxy library in a .net MVC 4 application. We are using structure map for our dependency injection. </p>
  
  <p>I have successfully set up AOP logging for our standard normal MVC controllers, but we also have a folder with WebAPI controllers that we also use. </p>
  
  <p>The problem I have is that for any of the WEBApi calls I get the following error</p>
  
  <pre><code>"Unable to cast object of type 'Castle.Proxies.IHttpControllerProxy' to type 'Web.Controllers.Services.Home.apiContollerName'.","ExceptionType":"System.InvalidCastException"
  </code></pre>
  
  <p>Here are the set details of my interceptor set-up</p>
  
  <pre><code>//Logging Interceptor in strucutremap  ObjectFactory.Initialize
  x.RegisterInterceptor(new LogTypeInterceptor());
  </code></pre>
  
  <p>Here is my Process Method</p>
  
  <pre><code>public object Process(object target, IContext context)
      {
          var obj = new object();
  
          obj = _proxyGenerator.CreateInterfaceProxyWithTargetInterface(
          target.GetType().GetInterfaces().First(),
          target.GetType().GetInterfaces(),
          target, new LoggingInterceptor());
  
          return obj;
      }
  </code></pre>
  
  <p>I suspect I need to call a different method on _proxyGenerator but i'm new to this not sure what method I should call.</p>