# Dependency Injection in Filters

**URL:** <https://discuss.hangfire.io/t/dependency-injection-in-filters/2324>\
**Category:** question\
**Created:** [August 16, 2016, 2:55pm UTC](https://discuss.hangfire.io/t/dependency-injection-in-filters/2324 "2016-08-16T14:55:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![JVaughan](https://avatars.discourse-cdn.com/v4/letter/j/3ec8ea/32.png) [@JVaughan](https://discuss.hangfire.io/u/JVaughan)\
**Post date:** [August 16, 2016, 2:55pm UTC](https://discuss.hangfire.io/t/dependency-injection-in-filters/2324/1 "2016-08-16T14:55:01Z")

</div>

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.
