Autofac registered AuthorizationFilter calling twice

asp.net-web-api, autofac, c#

Solution

The line:

builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);

will register anything that implements the `IAutofacAuthorizationFilter` interface. So strictly speaking, you shouldn't need the second line.

The second line just re-registers what Autofac is already automatically doing in the line above. So, remove the second line.

Problem

Hi I have an Authorization filter, created using Autofac support for WebApi. Summary as follows: ``` public class ApplicationTokenValidatorAttribute : IAutofacAuthorizationFilter { //... /// <summary> /// Default constructor for ApplicationTokenValidatorAttribute /// </summary> /// <param name="tenancyClient">Tenancy service used to resolve application key checks and populate tenancy object on valid controllers</param> /// <param name="commonServices">Provides access to commonly used services, including logging and performance tracing</param> public ApplicationTokenValidatorAttribute(ITenancyClient tenancyClient, ICommonServices commonServices) { _tenancyClient = tenancyClient; _commonServices = commonServices; } /// <summary> /// Pass/fails authentication, based on whether you provide a valid application key in the http headers of the request /// </summary> /// <param name="actionContext">Action filter context</param> public void OnAuthorization(HttpActionContext actionContext) { //... } ``` I don't believe the actual code in the filter is important, but if anyone thinks otherwise, then I can sanity check it and paste more. I register it using this autofac code ``` builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration); builder.Register(c => new ApplicationTokenValidatorAttribute( c.Resolve<ITenancyClient>(), c.Resolve<ICommonServices>())) .AsWebApiAuthorizationFilterFor<TenantAwareApiController>() .InstancePerApiRequest(); ``` which is I think as laid out in https://code.google.com/p/autofac/wiki/WebApiIntegration. If I do it like this however, it is called twice for every request. If I comment out the line ` builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration); ` then the filter is only called once, as expected. Can anyone shed some light on this? DO I need this line? I'm loath to exclude the statement specifically listed in the documentation, but it seems to be the source of the issue. cheers, P

Original source