的WebAPI行动过滤器调用两次 [英] WebApi Action filter called twice

查看:2435
本文介绍了的WebAPI行动过滤器调用两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WebAPI滤波方法 OnActionExecuted 被调用两次。
我的过滤器(我让它尽可能简单):

 公共类NHibernateActionFilter:ActionFilterAttribute
   {
        // [注入]
        //公共ISessionFactoryProvider sessionFactoryProvider {搞定;组; }
        公共覆盖无效OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
           VAR一个= 5;
           变种B = A;
           //新BaseSessionProvider(sessionFactoryProvider).EndContextSession();
        }
    }

我的设置:

 保护无效的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);
    }    公共类一个FilterConfig
    {        公共静态无效RegisterWebApiFilters(System.Web.Http.Filters.HttpFilterCollection过滤器)
        {
             filters.Add(新NHibernateActionFilter());
        }
     }

在调试器中我抓住 OnActionExecuted 用相同的两次 actionExecutedContext 。为什么呢?

UPD

 控制器
公共类BankSmsController:ApiController
{
         的[AcceptVerbs(HttpVerbs.Get)
         公众诠释GetTest()
         {
             返回1;
         }
}


解决方案

我怀疑,这个奇怪的行为可以通过过滤器的任何覆盖的AllowMultiple 属性是固定的,返回false,或将 AttributeUsage 属性与的AllowMultiple 设置为false太(上<$ C $的默认实现这种影响C>的AllowMultiple 过滤器的属性。

至少在我们这个项目帮助(我们有通过注入Autofac过滤器)。

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;
         }
}

解决方案

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).

这篇关于的WebAPI行动过滤器调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆