Still learning filters and attributes. I tried these steps below, and it seems to be working. Can someone please review and confirm if it’s the right approach?
- Separate the attribute and filter classes:
public class MyHangfireFilterAttribute : JobFilterAttribute
{
// no implementation (empty class)
}
public class MyHangfireFilter: IClientFilter, IServerFilter, IElectStateFilter
{
private readonly SomeDependency _dependency;
public MyHangfireFilter(SomeDependency someDependency)
{
_dependency = someDependency;
}
// further implementation...
}
- Register the filter:
services.AddHangfire((provider, config) =>
config.UseFilter(new MyHangfireFilter(provider.GetService<SomeDependency>())));
- Decorate the job method with the attribute:
public class MyHangfireJobs
{
[MyHangfireFilter]
public void DoSomething()
{
}
}
So, separating the attribute and filter classes, but following their naming convention (NameFilterAttribute
for attribute class and NameFilter
for filter class) did the trick. An article about passive attributes in .NET helped.
Can someone please review and confirm if it’s the right approach?