Hi,
I’m implementing some logic that runs after the execution of every job. At first, the Job Filters functionality seemed ideal, however I soon realised that it suffers from the same problems as filters in ASP MVC (due to the same architecture / concepts), such that any class implementing the filters cannot be used for dependency injection.
I have worked out a solution that involves create a custom IJobFilterProvider which is capable of grabbing my custom filter from the DI container. This custom provider was then registered with the (undocumented) JobFilterProviders like so:
var container = new Container(...);
JobFilterProviders.Providers.Add(new StructureMapAuditFilterProvider(container));
where StructrueMapAuditFilterProvider looks like:
public class StructureMapAuditFilterProvider : IJobFilterProvider {
private readonly IContainer _container;
public StructureMapAuditFilterProvider(IContainer container) {
_container = container;
}
IEnumerable<JobFilter> IJobFilterProvider.GetFilters(Job job) {
var auditFilter = _container.GetInstance(typeof (AuditingFilter));
yield return new JobFilter(auditFilter, JobFilterScope.Global, null);
}
}
While this does work for me, I don’t feel like it’s a solution that fits with Hangfire and feel there should be a better way than using my DI container to manually resolve an instance.
Is there a better way to achieve this?
Thanks.