Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebApi Action filter called twice

My WebApi filter method OnActionExecuted is being called twice. My filter (I make it as simple as possible):

   public class NHibernateActionFilter : ActionFilterAttribute
   { 
        //  [Inject]
        //   public ISessionFactoryProvider sessionFactoryProvider { get; set; }
        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
           var a = 5;
           var b = a;
           //new BaseSessionProvider(sessionFactoryProvider).EndContextSession();
        }
    }

My setup:

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        WebApiConfig.Register(GlobalConfiguration.Configuration);
        //http://stackoverflow.com/questions/9521040/how-to-add-global-asp-net-web-api-filters
        FilterConfig.RegisterWebApiFilters(GlobalConfiguration.Configuration.Filters);
    }

    public class FilterConfig
    {

        public static void RegisterWebApiFilters(System.Web.Http.Filters.HttpFilterCollection filters)
        {
             filters.Add(new NHibernateActionFilter());
        }
     }

In debugger I catch OnActionExecuted twice with the same actionExecutedContext. Why?

UPD

Controller
public class BankSmsController : ApiController
{
         [AcceptVerbs(HttpVerbs.Get)]
         public int GetTest()
         {
             return 1;
         }
}
like image 784
Andrew Kalashnikov Avatar asked Aug 28 '13 10:08

Andrew Kalashnikov


1 Answers

I have a suspicion, that this strange behavior can be fixed by either overriding AllowMultiple property of filter and returning false, or applying AttributeUsage attribute with AllowMultiple set to false too (this influences on default implementation of AllowMultiple property of filter.

At least in our project this helped (we have filters injected via Autofac).

like image 170
Mikhail Brinchuk Avatar answered Oct 20 '22 23:10

Mikhail Brinchuk