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...
GlobalConfiguration.Configuration.UseStructureMapActivator(container);
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.
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.
container.For<IDatabase>().ContainerScoped().Use<Database>();
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()
{
_dependency.Dispose();
}
}
container.For<JobClass>().ContainerScoped();
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