ServiceFilterAttribute lets an ASP.NET Core MVC action or controller use a filter whose instance and dependencies come from the dependency-injection container. Register the filter and its dependencies, then apply the attribute where the filter should run. section 22.3 section 22.4
Applying a registered filter
Register the filter with DI: builder.Services.AddTransient<EnsureRecipeExistsFilter>(); Then decorate an action: [ServiceFilter(typeof(EnsureRecipeExistsFilter))] public IActionResult Index() => Ok(); The filter can receive services such as RecipeService through its constructor. section 22.3
Use a separate filter class for behavior and an attribute for placement. This avoids the limitation that C# attributes cannot receive ordinary service dependencies in their constructors and are created as singleton instances. section 22.3
Figure 22.3 The framework scans your app on startup to find both filters and attributes that implement IFilterFactory. At runtime, the framework calls CreateInstance() to get an instance of the filter.
The framework discovers filter factories during startup and calls CreateInstance() at runtime to obtain the filter instance. section 22.3
