Web API 2的Autofac IAutofacActionFilter执行顺序 [英] Autofac IAutofacActionFilter execution order for Web API 2

查看:151
本文介绍了Web API 2的Autofac IAutofacActionFilter执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以设置在Autofac注册的Web api操作过滤器上的执行顺序? 目前,如果我注册以下内容:

Are there any ways to set the order of execution on web api action filters registered with Autofac? Currently, if I register the following:

builder.Register(x => new MyFirstAttribute(x.Resolve<IMyService>())).AsWebApiActionFilterFor<ApiController>().InstancePerRequest();
builder.Register(x => new MySecondAttribute(x.Resolve<IMyService>())).AsWebApiActionFilterFor<ApiController>().InstancePerRequest();

这是未知",它将首先执行,然后执行.即使我创建一个以特定方式订购它们的新FilterProvider,由于

it is 'unknown' which will be executing first and second. Even if I'd create a new FilterProvider which orders them in a specific way it will not work due to removal of any custom IFilterProvider and a private ActionDescriptorFilterProvider.

推荐答案

当前无法手动指定已注册过滤器的运行顺序.

There is currently no way to manually specify the order in which registered filters run.

使用IEnumerable<T> 内隐关系类型.这种情况在AutofacWebApiFilter中一个>.与常规过滤器一样,它不仅比按XYZ顺序运行"还要复杂-还需要考虑控制器作用域和动作作用域.

Filters get resolved in the same way other dependencies get resolved using the IEnumerable<T> implicit relationship type. This happens in the AutofacWebApiFilterProvider. As with regular filters, it's more complex than just "run in XYZ order" - there's controller vs. action scoping to take into account as well.

所以,说您注册了这些:

So, say you registered these:

builder.RegisterType<LoggingFilter>()
    .AsWebApiActionFilterFor<ValuesController>()
    .InstancePerApiRequest();
builder.RegisterType<AuthenticationFilter>()
    .AsWebApiActionFilterFor<ValuesController>()
    .InstancePerApiRequest();
builder.RegisterType<ErrorFilter>()
    .AsWebApiActionFilterFor<ValuesController>(c => c.Get(default(int)))
    .InstancePerApiRequest();
builder.RegisterType<RoundingFilter>()
    .AsWebApiActionFilterFor<ValuesController>(c => c.Get(default(int)))
    .InstancePerApiRequest();

Web API过滤器先运行控制器级别,然后运行操作级别; Autofac筛选器以相反的注册顺序运行.如果有人在ValuesController上调用Get操作,则过滤器将运行:

Web API filters run controller level then action level; Autofac filters run in reverse registration order. If someone calls the Get action on the ValuesController the filters will run:

  • AuthenticationFilter
  • LoggingFilter
  • RoundingFilter
  • ErrorFilter

但是,有时并没有那么简单,因为如果使用扩展名,例如PreserveExistingDefaults,它会在内部更改注册顺序,从而将PreserveExistingDefaults注册放在最后.

However, it's sometimes not as straightforward as that, since if you use an extension like PreserveExistingDefaults it internally changes the order of registrations to put the PreserveExistingDefaults registration last.

这种处理默认设置等问题的注册排序复杂性是为什么我不能将您指向一行的代码.您可以查看 href ="https://github.com/autofac/Autofac/blob/e70f7248b16e8603b3fc01446f5aa9e078c6c76a/src/Autofac/Features/Collections/CollectionRegistrationSource.cs" rel ="nofollow"> CollectionRegistrationSource ,它负责解决集合作为过滤器提供程序的一部分.您还可以在过滤器提供程序中查看/a>看看事情如何发生.

That sort of registration ordering complexity with handling defaults and so forth is why I can't point you to a line of code.You can look at the CollectionRegistrationSource which is responsible for resolving the IEnumerable<IActionFilter> collections as part of the filter provider. You can also look at the filter provider to see how things happen.

如果需要手动指定顺序,则必须将自己的扩展名写入AutofacWebApiFilterProvider,或者,如果扩展名不起作用,请自行滚动.如果您以流畅的方式使其运作良好,我们很乐意接受请求请求.

If you need to manually specify order, you'll have to write your own extension to the AutofacWebApiFilterProvider or, if extension doesn't work, roll your own. If you get it working well in a nice fluent manner, we'd be happy to take a pull request.

这篇关于Web API 2的Autofac IAutofacActionFilter执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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