Hangfire background job activator based on the StructureMap IoC container
C# Batchfile
Latest commit 017f9fc Mar 3, 2016 @cocowalla Fix typo



StructureMap support for Hangfire. Provides an implementation of the JobActivator class and plugin family expression extensions, allowing you to use StructureMap IoC container to resolve job type instances as well as control the lifetime of the related dependencies.


Hangfire.StructureMap is available as a NuGet Package. Type the following command into NuGet Package Manager Console window to install it:

Install-Package Hangfire.StructureMap


The package provides an extension method for the IGlobalConfiguration interface, so you can enable StructureMap integration using the GlobalConfiguration class.

var container = new Container();
// container.Configure...


After invoking the methods above, StructureMap-based implementation of the JobActivator class will be used to resolve job type instances and all their dependencies during the background processing.

Re-using Dependencies

Sometimes it is necessary to re-use instances that are already created, such as database connection, unit of work, etc. Thanks to the ContainerLifecycle feature of StructureMap, you are able to do this by resolving objects from the nested container.

Hangfire.StructureMap doesn't rely on a specific lifecycle. To allow you to limit the object scope to the current background job processing, use the ContainerScoped extension method when configuring your dependencies.


Deterministic Disposal

All the dependencies that implement the IDisposable interface are disposed as soon as current background job is performed, but only when they were registered with the ContainerLifecycle lifecycle. For other cases, please read about object lifecycles.

For most typical cases, you can call the ContainerScoped extension method on a job plugin family expression and implement the Dispose method that will dispose all the dependencies manually:

public class JobClass : IDisposable
    private readonly Dependency _dependency;

    public JobClass(Dependency dependency) { /* ... */ }

    public Dispose()

HTTP Request warnings

Services registered with HttpContextScoped() directive or the HttpContextLifecycle lifecycle will be unavailable during job activation, you should re-register these services without this hint.

HttpContext.Current is also not available during the job performance. Don't use it!


This package takes advantage of nested containers. The StructureMapJobActivator creates a nested container for the StructureMapDependencyScope which is based on Hangfire's JobActivatorScope