Autofac (WebApi) IAutofacExceptionFilter OnException called twice

2.1k views Asked by At

I am having a problem using autofac for web api. I have a custom ExceptionFilterAttribute which overrides OnException to log and process any exceptions that may occur. The ExceptionFilterAttribute is applied to one ApiController class.

The filter code is as below:

public class ExceptionFilterAttribute : ExceptionFilterAttribute, IAutofacExceptionFilter
{
    private ILogger _logger;

    public ExceptionFilterAttribute()
    {

    }

    public ExceptionFilterAttribute(ILogger logger)
    {
        _logger = logger;
    }

    public override void OnException(HttpActionExecutedContext context)
    {
        // Removed for brevity
    }
}

In the autofac configuration, there are a lot of types registered which I have removed from the below where they are similar:

var builder = new ContainerBuilder();
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterType<ReflectionClassReader>().As<IClassReader>();
builder.RegisterType<Logger>().As<ILogger>();

builder.RegisterModule<DataModule>();

builder.Register(c => new ExceptionFilterAttribute(c.Resolve<ILogger>()))
       .AsWebApiExceptionFilterFor<ApiController>().SingleInstance();

builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);

var container = builder.Build();
configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

When an exception is thrown and bubbles up to the ApiController, the ExceptionFilterAttribute.OnException method is called twice. The first time it is called, _logger is populated correctly by Autofac; the second, _logger is null.

I cannot work out why this is being called twice. There is no stack trace when I debug as it is an attribute.

I can only assume that it is being called twice due to some autofac configuration that I've done, but I'm completely stuck. I've tried altering the order of the configuration in line with some of the examples I've found online but to no avail.

The only real information I've found on using IAutofacExceptionFilter with webapi was at http://alexmg.com/post/2012/09/01/New-features-in-the-Autofac-MVC-4-and-Web-API-%28Beta%29-Integrations.

Has anyone any ideas? Many thanks!

2

There are 2 answers

0
Spock On

Try removing the line.

builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);

I think what happening is this causes to register the exception filter twice. If you have other filters that need to register, you have few options

a. Try using only the AutoFac builder to register those filters. I mean use only one mechanism register filters.

b. Create CustomFilter Provider which you can remove/add filters if they have been duplicated etc. (There are plenty of examples on the web, but out of scope for this question )

0
workabyte On

think the issue here is that you are using the attribute and filter at the same time. so would get called by api under the hood because of the attribute and that would be a null logger and then again by autofac via the filter registration and that would populate the logger as it was created by autofac

try updating the class to this

public class ExceptionFilterAttribute : IAutofacExceptionFilter

removing ExceptionFilterAttribute,