Global filters with dependency injection
See original GitHub issueThe documentation specifies⬇️
// Filter is instantiated by DI so you can get parameter by constructor injection.
But only shows how to add global filters where the instance is instantiated upfront
var app = ConsoleApp.Create(args, options =>
{
options.GlobalFilters = new ConsoleAppFilter[]
{
new MutextFilter() { Order = -9999 } ,
new LogRunningTimeFilter() { Oder = -9998 },
}
});
Is there a way to use Dependency Injection with global filters, that I’ve just missed from the documentation?
e.g.
// filter class
public class MyFilter : ConsoleAppFilter
{
private readonly ILogger<MyFilter> logger;
public MyFilter(ILogger<MyFilter> logger)
{
this.logger = logger;
}
public override async ValueTask Invoke(ConsoleAppContext context, Func<ConsoleAppContext, ValueTask> next)
{
logger.LogInformation(context.MethodInfo.Name);
await next(context);
}
}
// and adding the filter
var builder = ConsoleApp.CreateBuilder(args, options =>
{
options.AddFilter<MyFilter>;
});
Issue Analytics
- State:
- Created a year ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
How to use dependency injection in action filters in ASP. ...
Use global action filters in ASP.NET Core 3.1 This enables you to apply the filter to every action method of every controller in...
Read more >ASP.NET Core Filters - Dependency Injection, Global ...
Filters run in a specific sequence : authorization, action, and then result. If multiple filters of a given type are applied to the...
Read more >dependency injection in mvc filter which is global
Wen you are registering global filters like this filters.Add(new MyActionFilterAttribute()); you are registering filter instance.
Read more >Dependency Injection in action filters in ASP.NET ... - DevTrends
Using ServiceFilter is a relatively clean way to allow dependency injection into individual action filters.
Read more >Filters in ASP.NET Core
Dependency injection. Filters can be added by type or by instance. If an instance is added, that instance is used for every request....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Still a thing
This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 30 days.