1. <legend id='6t6ew'><style id='6t6ew'><dir id='6t6ew'><q id='6t6ew'></q></dir></style></legend>
      <bdo id='6t6ew'></bdo><ul id='6t6ew'></ul>

    <small id='6t6ew'></small><noframes id='6t6ew'>

  2. <tfoot id='6t6ew'></tfoot>

    <i id='6t6ew'><tr id='6t6ew'><dt id='6t6ew'><q id='6t6ew'><span id='6t6ew'><b id='6t6ew'><form id='6t6ew'><ins id='6t6ew'></ins><ul id='6t6ew'></ul><sub id='6t6ew'></sub></form><legend id='6t6ew'></legend><bdo id='6t6ew'><pre id='6t6ew'><center id='6t6ew'></center></pre></bdo></b><th id='6t6ew'></th></span></q></dt></tr></i><div id='6t6ew'><tfoot id='6t6ew'></tfoot><dl id='6t6ew'><fieldset id='6t6ew'></fieldset></dl></div>
    1. ServiceFilter 和 TypeFilter - 注入这些过滤器有什么区别?

      ServiceFilter and TypeFilter - what is the difference in injection those filters?(ServiceFilter 和 TypeFilter - 注入这些过滤器有什么区别?)

      <small id='MULUc'></small><noframes id='MULUc'>

          <tbody id='MULUc'></tbody>
        <legend id='MULUc'><style id='MULUc'><dir id='MULUc'><q id='MULUc'></q></dir></style></legend>
          <bdo id='MULUc'></bdo><ul id='MULUc'></ul>
          <i id='MULUc'><tr id='MULUc'><dt id='MULUc'><q id='MULUc'><span id='MULUc'><b id='MULUc'><form id='MULUc'><ins id='MULUc'></ins><ul id='MULUc'></ul><sub id='MULUc'></sub></form><legend id='MULUc'></legend><bdo id='MULUc'><pre id='MULUc'><center id='MULUc'></center></pre></bdo></b><th id='MULUc'></th></span></q></dt></tr></i><div id='MULUc'><tfoot id='MULUc'></tfoot><dl id='MULUc'><fieldset id='MULUc'></fieldset></dl></div>
        • <tfoot id='MULUc'></tfoot>

                本文介绍了ServiceFilter 和 TypeFilter - 注入这些过滤器有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                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.

                要记住的关键事项

                我们要通过ServiceFilterService 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 - 注入这些过滤器有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                Accessing attribute info from DTE(从 DTE 访问属性信息)
                c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                C# Attributes and their uses(C# 属性及其用途)
                C# - Getting all enums value by attribute(C# - 按属性获取所有枚举值)
              1. <i id='QdY5P'><tr id='QdY5P'><dt id='QdY5P'><q id='QdY5P'><span id='QdY5P'><b id='QdY5P'><form id='QdY5P'><ins id='QdY5P'></ins><ul id='QdY5P'></ul><sub id='QdY5P'></sub></form><legend id='QdY5P'></legend><bdo id='QdY5P'><pre id='QdY5P'><center id='QdY5P'></center></pre></bdo></b><th id='QdY5P'></th></span></q></dt></tr></i><div id='QdY5P'><tfoot id='QdY5P'></tfoot><dl id='QdY5P'><fieldset id='QdY5P'></fieldset></dl></div>

                    <tbody id='QdY5P'></tbody>
                • <tfoot id='QdY5P'></tfoot>

                      <legend id='QdY5P'><style id='QdY5P'><dir id='QdY5P'><q id='QdY5P'></q></dir></style></legend>
                    1. <small id='QdY5P'></small><noframes id='QdY5P'>

                      • <bdo id='QdY5P'></bdo><ul id='QdY5P'></ul>