ServiceFilter and TypeFilter - what is the difference in injection those filters?

前端 未结 4 1046
轻奢々
轻奢々 2020-12-30 03:14

ServiceFilter we must register in Startup.cs. TypeFilter is injected by Microsoft.Extensions.DependencyInjection.ObjectFactory, we don\'t need register that filter.

<
4条回答
  •  有刺的猬
    2020-12-30 03:50

    Ok, so documentation:

    • A ServiceFilter retrieves an instance of the filter from DI. Using ServiceFilter without registering the filter type results in an exception.

    • TypeFilterAttribute is very similar to ServiceFilterAttribute (and also implements IFilterFactory), but its type is not resolved directly from the DI container. Instead, it instantiates the type by using Microsoft.Extensions.DependencyInjection.ObjectFactory.

    Because of this difference, types that are referenced using the TypeFilterAttribute do not need to be registered with the container first (but they will still have their dependencies fulfilled by the container).

提交回复
热议问题