question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Global filters with dependency injection

See original GitHub issue

The 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:open
  • Created a year ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
wozzocommented, Nov 4, 2022

Still a thing

0reactions
github-actions[bot]commented, Aug 7, 2023

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found