Dependency injection for filter attribute (non-global)

Hey @alien, if you’re using .NET Core’s builtin DI then you can do it like this:

  1. Register the class/interface service in Startup.cs (web) or Program.cs (console):

    services.AddTransient<SomeDependencyClass>();

    Note: change service lifetime to scoped or singleton as per your needs.

  2. Register filter instance with dependency in Program.cs (required for console app only; not required for web):

    services.AddHangfire((provider, config) => config.UseFilter(new MyFilterAttribute(provider.GetService<SomeDependencyClass>())));

Hope it helps.