title: Autofac global exception filter throwing null reference exception
body: <p>I'm trying to add a <strong>global</strong> exception handling filter that will log errors in an MVC 5 app.  I need to use Autofac to inject an error logging service into the filter to log the errors.  I have been using Autofac in this app for some time to inject my services and repositories without issue.</p>
  
  <p>When I follow the procedures outlined <a href="https://code.google.com/p/autofac/wiki/MvcIntegration#Inject_Properties_Into_FilterAttributes" rel="nofollow noreferrer">here</a> and <a href="https://stackoverflow.com/questions/13346801/register-global-filters-in-asp-net-mvc-4-and-autofac">here</a> I am met with a null reference exception:</p>
  
  <blockquote>
    <p>[NullReferenceException: Object reference not set to an instance of an
    object.]<br>
    Autofac.Integration.Mvc.AutofacFilterProvider.GetFilters(ControllerContext
    controllerContext, ActionDescriptor actionDescriptor) +206<br>
    System.Web.Mvc.FilterProviderCollection.GetFilters(ControllerContext
    controllerContext, ActionDescriptor actionDescriptor) +182<br>
    System.Web.Mvc.ControllerActionInvoker.GetFilters(ControllerContext
    controllerContext, ActionDescriptor actionDescriptor) +37
    ...</p>
  </blockquote>
  
  <p>I have removed the global filter registration for this attribute in the global.asax:</p>
  
  <pre><code>public static void RegisterGlobalFilters(GlobalFilterCollection filters)
  {
          filters.Add(new AuthorizeAttribute());
          //filters.Add(new HandleExceptionAttribute());
  }
  </code></pre>
  
  <p>And setup my Autofac dependency registration:</p>
  
  <pre><code>builder.RegisterControllers(Assembly.GetExecutingAssembly());
  builder.RegisterAssemblyTypes(typeof(BaseService).Assembly)
      .Where(t =&gt; t.Name.EndsWith("Service"))
      .AsImplementedInterfaces()
      .PropertiesAutowired();
  builder.RegisterFilterProvider();
  builder.RegisterType&lt;HandleExceptionAttribute&gt;()
      .AsExceptionFilterFor&lt;BaseController&gt;()
      .InstancePerHttpRequest()
      .PropertiesAutowired();
  </code></pre>
  
  <p>And my Handle Exception Attribute with constructor injection:</p>
  
  <pre><code>public class HandleExceptionAttribute : HandleErrorAttribute
  {
      private IErrorLoggingService Service { get; set; }
  
      public HandleExceptionAttribute(IErrorLoggingService service)
      {
          this.Service = service;
      }
  
      public override void OnException(ExceptionContext filterContext)
      {
          this.Service.LogError(new Error());
      }
  }
  </code></pre>
  
  <p>I've done a lot of Googling on this subject but was not able to find an exact example of what I'm looking for.  A few results indicated an issue with also having Glimpse installed, which I had, but since removed.  Calling builder.RegisterFilterProvider(); inside of the Autofac setup is the line that causes the exception.</p>
  
  <p>Am I missing something or is this just not possible?  Does anyone have a working example?  Thanks!</p>
  
  <p>Edit:
  I have another global filter applied (AuthorizeAttribute) and I tried removing it but I still get the null ref exception.  Another thought was that I'm using other nuget packages that could cause conflicts, like Glimpse has in the past.
  Here are other nuget packages I am using:</p>
  
  <pre><code>&lt;packages&gt;
    &lt;package id="AmplifyJS" version="1.1.0" targetFramework="net45" /&gt;
    &lt;package id="angularjs" version="1.2.16" targetFramework="net45" /&gt;
    &lt;package id="Antlr" version="3.5.0.2" targetFramework="net45" /&gt;
    &lt;package id="Autofac" version="3.3.1" targetFramework="net45" /&gt;
    &lt;package id="Autofac.Mvc5" version="3.2.1" targetFramework="net45" /&gt;
    &lt;package id="CsQuery" version="1.3.4" targetFramework="net45" /&gt;
    &lt;package id="EntityFramework" version="6.1.0" targetFramework="net45" /&gt;
    &lt;package id="jQuery" version="2.1.0" targetFramework="net45" /&gt;
    &lt;package id="jQuery.Migrate" version="1.2.1" targetFramework="net45" /&gt;
    &lt;package id="jQuery.Validation" version="1.12.0" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.AspNet.Mvc" version="5.1.2" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.AspNet.Razor" version="3.1.2" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.AspNet.WebPages" version="3.1.2" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.jQuery.Unobtrusive.Ajax" version="3.1.2" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.1.2" targetFramework="net45" /&gt;
    &lt;package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" /&gt;
    &lt;package id="MicrosoftWebMvc" version="2.0" targetFramework="net45" /&gt;
    &lt;package id="MoxieManager" version="1.0" targetFramework="net45" /&gt;
    &lt;package id="Mvc2Futures" version="2.0.50217.0" targetFramework="net45" /&gt;
    &lt;package id="MvcContrib" version="2.0.95.0" targetFramework="net45" /&gt;
    &lt;package id="Newtonsoft.Json" version="6.0.2" targetFramework="net45" /&gt;
    &lt;package id="PreMailer.Net" version="1.2.4" targetFramework="net45" /&gt;
    &lt;package id="WebGrease" version="1.6.0" targetFramework="net45" /&gt;
  &lt;/packages&gt;
  </code></pre>
  
  <p>I will try an MVC project from scratch as you suggested to see what happens.</p>