问题描述
ServiceFilter 我们必须在 Startup.cs 中注册.TypeFilter 由 Microsoft.Extensions.DependencyInjection.ObjectFactory 注入,我们不需要注册该过滤器.
ServiceFilter we must register in Startup.cs. TypeFilter is injected by Microsoft.Extensions.DependencyInjection.ObjectFactory, we don't need register that filter.
那么我们什么时候应该使用 ServiceFilter 以及什么时候使用 TypeFilter 呢?
So when we should use ServiceFilter and when TypeFilter ?
推荐答案
根据Pro ASP.NET核心 MVC 2 书.第 19 章:过滤器,第 615 页
According to Pro ASP.NET Core MVC 2 book. Chapter 19: Filters, page # 615
当使用 TypeFilter 属性时,过滤器的新实例为每个请求创建类.这是相同的行为直接将过滤器作为属性应用,除了 TypeFilter属性允许过滤器类声明依赖关系通过服务商解决.服务过滤器属性更进一步,使用服务提供者创建过滤器目的.这允许将过滤器对象放置在生命周期下管理也是如此.
When using the TypeFilter attribute, a new instance of the filter class is created for every request. This is the same behavior as applying a filter directly as an attribute, except that the TypeFilter attribute allows a filter class to declare dependencies that are resolved through the service provider. The ServiceFilter attribute goes a step further and uses the service provider to create the filter object. This allows filter objects to be placed under life-cycle management as well.
区别由于 ServiceFilter
使用 ServiceProvider
来解析相关过滤器的实例,因此您可以控制在启动类中注册的过滤器的生命周期:
THE DIFFERENCE
Since the ServiceFilter
uses the ServiceProvider
to resolve the instance of the filter in question, you have control over the lifecycle of the filter which is registered in the startup class:
services.AddSingleton<TimeFilter>();
从上面的代码行中,TimeFilter
将只为 MVC 应用程序生命周期创建一次(不是为每个 http 请求生命周期或客户端请求时),它将服务于所有 http使用 TypeFilter
不可能的请求,因为您无法指示 MVC 框架何时实例化和处置在 TypeFilter
下使用的过滤器.
From above line of code, the TimeFilter
will only be created once for the MVC application lifecycle (not for each http request life cycle or when client asks for it) that will serve for all the http requests which is not possible using TypeFilter
because there is no way you can instruct MVC framework when to instantiate and dispose the filter used under TypeFilter
.
如果过滤器注册为 Singleton
则只创建该过滤器的一个实例,这意味着 CLR 的工作量更少,这与 TypeFilter
创建新实例的情况不同每个 http 请求的过滤器类.
If the filter is registered as Singleton
then only one instance of that filter is created which means less work for CLR which is unlike in case of TypeFilter
that creates new instance of filter class for each http request.
用法假设您在两个操作方法上应用了 TypeFilter
,对于每个 HTTP 请求,将创建该 TypeFilter
的新实例,将调用构造函数并注入依赖项(您可以使用 Service Provider
控制依赖项的生命周期).相反,使用 ServiceFilter
您可以决定它是 Singleton
还是 Scoped
还是 Transient
.如果它的 Singleton
则只为所有请求创建一个实例.
THE USAGE
Say you have a TypeFilter
applied on two action methods, for each HTTP request, a new instance of that TypeFilter
will be created, the constructor will be called and dependencies will be injected (you can control the life cycle of dependencies using the Service Provider
). In contrast, with ServiceFilter
you decide if its Singleton
or Scoped
or Transient
. If its Singleton
then only one instance is created for all the requests.
要记住的关键事项
我们要通过ServiceFilter
和Service Provider
来管理过滤器类型的生命周期.如果过滤器有依赖关系,我们已经像往常一样使用 Service Provider
来管理它.
It’s the filter type’s life cycle that we want to manage by using ServiceFilter
and Service Provider
. If the filter has dependencies, we already manage that using Service Provider
like we normally do.
这篇关于ServiceFilter 和 TypeFilter - 注入这些过滤器有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!