[英]Some questions about webapi ActionFilterAttribtes, FilterProvider, and Dependency Injection
I see that by default, an MVC4 WebApi project has 2 IFilterProviders
: The ConfigurationFilterProvider
, and the ActionDescriptorFilterProvider
. 我看到默认情况下,一个MVC4 WebApi项目具有2个
IFilterProviders
: ConfigurationFilterProvider
和ActionDescriptorFilterProvider
。 When setting up a custom IFilterProvider
to do property depenency injection, is it appropriate to remove both of these from the Filters collection first? 设置自定义
IFilterProvider
进行属性依赖注入时,是否先从Filters集合中同时删除这两个属性? Or is it more appropriate to just remove the ActionDescriptorFilterProvider
, and leave the ConfigurationFilterProvider
in the collection? 还是仅删除
ActionDescriptorFilterProvider
并将ConfigurationFilterProvider
保留在集合中更合适?
Secondly, I see that after a WebApi Http ActionFilterAttribute
is constructed for a particular action, it seems to stay around. 其次,我看到为特定动作构造了WebApi Http
ActionFilterAttribute
之后,它似乎仍然存在。 For example when I put a custom action filter on an ApiController Get method, it is only constructed the first time that method is called. 例如,当我在ApiController的Get方法上放置自定义操作过滤器时,它只会在第一次调用该方法时构造。 Subsequent calls to the api method seem to reuse the same filter (constructor breakpoints are only hit during the first invocation).
随后对api方法的调用似乎重用了相同的过滤器(构造函数断点仅在第一次调用时命中)。 Is this right?
这是正确的吗? Why is it different than an MVC actionfilter, where a new instance is created for each method invocation?
为什么它与MVC操作过滤器不同,后者为每个方法调用创建一个新实例?
is it appropriate to remove both of these from the Filters collection first?
是否先从“过滤器”集合中同时删除这两项?
I have been successful in implementing dependency injection on Action Filters without removing either of these FilterProviders. 我已成功实现了对动作过滤器的依赖项注入,而没有删除这两个FilterProviders中的任何一个。 Infact, I am extending the
ActionDescriptorFilterProvider
in my example using StructureMap. 实际上,我在示例中使用StructureMap扩展了
ActionDescriptorFilterProvider
。
http://evolutionarydeveloper.blogspot.co.uk/2012/11/webapi-actionfilter-dependency.html http://evolutionarydeveloper.blogspot.co.uk/2012/11/webapi-actionfilter-dependency.html
I cannot shed any light on your second question I'm afraid :-) 恐怕我对您的第二个问题没有任何帮助:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.