ServiceFilter and TypeFilter - what is the difference in introducing these filters?

ServiceFilter we must register in Startup.cs. TypeFilter is introduced by Microsoft.Extensions.DependencyInjection.ObjectFactory, we do not need to register this filter.

So when should we use ServiceFilter and when is TypeFilter?

+6
source share
3 answers

So the documentation :

  • A ServiceFilterretrieves the filter instance from the DI. Use ServiceFilterof a filter type without registration results in an exception.

  • TypeFilterAttribute ServiceFilterAttribute ( IFilterFactory), , DI. Microsoft.Extensions.DependencyInjection.ObjectFactory.

- , TypeFilterAttribute , ( , ).

+5

ServiceFilter TypeFilter .

this, TypeFilter Microsoft.Extensions.DependencyInjection.ObjectFactory, ( ). , .

, - :

public class AttachMetadataAttribute : Attribute, IAsyncActionFilter
{
    public AttachMetadataAttribute(SomeType someValue, ISomeService service)
        {
        }
}

:

[TypeFilter(typeof(AttachMetadataAttribute),
    IsReusable = true,
    Order = 10,
    Arguments = new object[] { someValue})]

, (someValue) , .

. IsReusable. true, .

+2

, , TypeFilterAttribute. .

0

Source: https://habr.com/ru/post/1016825/


All Articles