4

How do I set up my Web API 2 (version 5) project hosted in IIS to log an error if dependency resolution for a controller fails? I’m using the Autofac Web API 2 integration (version 3.1) for DI.

In my Global.asax.cs I have the following:

public class Global : HttpApplication {
    protected void Application_Start() {
        var resolver = new AutofacWebApiDependencyResolver(_container);
        GlobalConfiguration.Configuration.DependencyResolver = resolver;

        // other initialization

        GlobalConfiguration.Configuration.EnsureInitialized(); 

    }

    protected void Application_Error(Object sender, EventArgs e){
        Exception exception = Server.GetLastError();
        if (exception != null) {
            _log.Error("Application error", exception);
        }
    }
}

In a similar MVC project using AutofacDependencyResolver this suffices to log the Autofac.Core.DependencyResolutionException thrown when Autofac can’t resolve a dependency while creating a controller, but in my Web API project this doesn’t seem to work, either the exception isn’t thrown or I don’t have the correct logging handler set up. Instead no message is logged an a 500 response is returned to the client.

If no errors are thrown during dependency resolution (my module is set up correctly) then everything works fine, the controller is resolved and handles the request as expected.

I also have a couple other exception loggers set up that also don’t log this exception:

  • Handling the unhandled exception event: AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException (in Global.asax.cs)
  • Setting up an exception logging filter deriving from ExceptionFilterAttribute. This logs exceptions thrown while the controller is handling the request but not this one thrown during dependency resolution.