Hangfire.Autofac 2.6.0

Prefix Reserved
dotnet add package Hangfire.Autofac --version 2.6.0                
NuGet\Install-Package Hangfire.Autofac -Version 2.6.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Hangfire.Autofac" Version="2.6.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Hangfire.Autofac --version 2.6.0                
#r "nuget: Hangfire.Autofac, 2.6.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Hangfire.Autofac as a Cake Addin
#addin nuget:?package=Hangfire.Autofac&version=2.6.0

// Install Hangfire.Autofac as a Cake Tool
#tool nuget:?package=Hangfire.Autofac&version=2.6.0                

Hangfire.Autofac Build status

Autofac integration for Hangfire. Provides an implementation of the JobActivator class and registration extensions, allowing you to use Autofac container to resolve job type instances as well as control the lifetime of the all related dependencies.

Hangfire.Autofac resolves service instances using a child, tagged lifetime scope. A child scope is created and disposed each time when background job processing takes place, so you have precise control of your service's lifetime, including shared instances and deterministic disposal.

Installation

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

> dotnet add package Hangfire.Autofac

Usage

The package provides an extension methods for the IGlobalConfiguration interface, so you can enable Autofac integration using the GlobalConfiguration class:

var builder = new ContainerBuilder();
// builder.Register...

GlobalConfiguration.Configuration.UseAutofacActivator(builder.Build());

After invoking the UseAutofacActivator method, Autofac-based implementation of the JobActivator class will be used to resolve job type instances during the background job processing.

Shared Components

Sometimes it is required to share the same service instance for different components, such as database connection, unit of work, etc. Hangfire.Autofac allows you to share them in a scope, limited to the current background job processing, just call the InstancePerBackgroundJob method in your component registration logic:

builder.RegisterType<Database>().InstancePerBackgroundJob();

Non-tagged scopes

Whenever the scopes in AutofacActivator are created, by default they are created using tag BackgroundJobScope. There might be a scenario when it is needed not to use tagged scopes though. This might be a case if it is required to have a new instance of every service for each lifetime scope (in Hangfire it would be for every job). To disable tagged scopes you can use a flag useTaggedLifetimeScope during initialization of AutofacActivator for Hangfire.

var builder = new ContainerBuilder();
// builder.Register...

GlobalConfiguration.Configuration.UseAutofacActivator(builder.Build(), false);

Then you can register services by using InstancePerLifetimeScope and expect them to work like intended.

builder.RegisterType<Database>().InstancePerLifetimeScope();

Deterministic Disposal

The child lifetime scope is disposed as soon as current background job is performed, successfully or with an exception. Since Autofac automatically disposes all the components that implement the IDisposable interface (if this feature not disabled), all of the resolved components will be disposed if appropriate.

For example, the following components will be disposed automatically:

builder.RegisterType<SmsService>();
builder.RegisterType<EmailService>().InstancePerDependency();
builder.RegisterType<Database>().InstancePerBackgroundJob();

And the following components will not be disposed:

builder.RegisterType<BackgroundJobClient>().SingleInstance();
builder.RegisterType<MyService>().ExternallyOwned();

Please refer to the Autofac documentation to learn more about Automatic Disposal feature.

Registering With Multiple Lifetime Scopes

Services registered with tagged lifetime scopes (eg InstancePerBackgroundJob, Autofac's InstancePerRequest or a scope your specific application requires) will not resolve outside of these named scopes, a common situation is when using Hangfire in an ASP.NET web application. In these situations you must register all your lifetimescopes together if you want the services to be resolved from any of the scopes. Hangfire.Autofac exposes it's lifetime tag and an overload of InstancePerBackgroundJob to help you do this.

To register a service with both Autofac's PerRequest and Hangfire's PerBackgroundJob you could do any of the following:

Passing Hangfire's scope tag to Autofac's InstancePerHttpRequest:

builder.RegisterType<SharedService>().InstancePerHttpRequest(AutofacJobActivator.LifetimeScopeTag);

From Autofac 3.4.0 Autofac exposed their lifetime tag, MatchingScopeLifetimeTags.RequestLifetimeScopeTag, which can be used with InstancePerBackgroundJob:

builder.RegisterType<SharedService>().InstancePerBackgroundJob(MatchingScopeLifetimeTags.RequestLifetimeScopeTag);

Both scope tags can also be used directly with Autofac's InstancePerMatchingLifetimeScope

var requestTag = MatchingScopeLifetimeTags.RequestLifetimeScopeTag;
var jobTag = AutofacJobActivator.LifetimeScopeTag;
builder.RegisterType<SharedService>().InstancePerMatchingLifetimeScope(requestTag, jobTag);

Mixed Lifetime Scopes

Beaware that if you are using multiple lifetime scopes to share services that all dependencies of those services need to be similarly regustered. For example:

public class WebOnlyService(){ ... }
public class SharedService(WebOnlyService){ ... }

builder.RegisterType<SharedService>().InstancePerRequest();
builder.RegisterType<SharedService>().InstancePerMatchingLifetimeScope(requestTag, jobTag);

Attempting to resolve SharedService from a background job will throw an exception as Autofac will need to resolve WebOnlyService outside of a RequestLifetimeScope.

Also be aware that many web related properties that you may be using such as HttpContext.Current will be unavailable.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.3 is compatible.  netstandard1.4 was computed.  netstandard1.5 was computed.  netstandard1.6 was computed.  netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Universal Windows Platform uap was computed.  uap10.0 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (19)

Showing the top 5 NuGet packages that depend on Hangfire.Autofac:

Package Downloads
Bnsights.Mvc

Bnsights.Mvc is RAD Helper DLL for MVC Projects in Bnsights DMCC. Also known as Bnsights Business Solutions Framework (BBSF).

Bnsights.Core

Package Description

Bnsights.Mvc2

Bnsights.Mvc is RAD Helper DLL for MVC Projects in Bnsights DMCC. Also known as Bnsights Business Solutions Framework (BBSF).

Bit.Hangfire

Bit.Hangfire

LeanCode.AsyncTasks.Hangfire

LeanCode Core library

GitHub repositories (3)

Showing the top 3 popular GitHub repositories that depend on Hangfire.Autofac:

Repository Stars
fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat
可能是全网最完整的 C# 版微信 SDK,封装全部已知的微信 OpenAPI,包含微信公众平台(订阅号+服务号+小程序+小游戏+小商店+视频号)、微信开放平台、微信商户平台(微信支付+微企付)、企业微信、微信广告平台、微信智能对话开放平台等模块,可跨平台。持续随官方更新,欢迎 Star/Fork/PR。QQ 交流群 875580418【满】、930461548【满】、611974621。
DevArchitecture/DevArchitecture
DevArchitecture Backend Project
sd797994/Oxygen-Dapr.EshopSample
Version Downloads Last updated
2.6.0 378,630 2/16/2024
2.5.0 39,738 1/23/2024
2.4.1 428,789 5/25/2023
2.4.0 811 5/25/2023
2.3.1 6,422,205 6/8/2017
2.3.0 3,962 6/7/2017
2.2.0 450,075 2/24/2016
2.1.0 17,403 12/16/2015
2.0.1 44,311 10/15/2015
2.0.0 25,136 10/1/2015
2.0.0-beta1 2,869 9/3/2015
1.2.0 1,961 10/1/2015
1.2.0-beta2 1,537 9/3/2015
1.2.0-beta1 1,872 7/22/2015
1.1.0 24,582 4/13/2015
1.0.0 11,177 6/30/2014
0.1.2 1,941 5/2/2014
0.1.0 2,480 4/1/2014

https://github.com/HangfireIO/Hangfire.Autofac/releases
2.6.0
* Project – Sign NuGet package and .NET assemblies on build.
* Project – Enable NuGet package signature validation on restore.
* Project – Use deterministic and locked package restore for all projects.
* Project – Add more metadata for assemblies and NuGet package.
* Project – Fix GitHub repository URL in the nuspec file.
* Project – Add `HangfireIO` as a NuGet package owner.

2.5.0
* Project – Full source link support with embedded symbols and repository-based sources.
* Project – Enable static analysis via the `Microsoft.CodeAnalysis.NetAnalyzers` package.
* Project – Restore packages in locked mode for all the projects.
* Project – Switch from MSTest to Xunit for simplicity across other projects.

2.4.1
* Fixed – Add missing files for the `netstandard2.0` target to the NuGet package.

2.4.0
* Added – `netstandard2.0` target support for the package.
   
2.3.1
* Changed – NuGet package title and a link to the project site were added.

2.3.0
* Added – Support for .NET Core with .NET Standard 1.3.
* Added – Support for SourceLink 2.0. If "Enable source link support flag is set" in Visual Studio it's possible to debug Hangfire.Autofac using its downloaded sources.
* Changed – Removed references to Newtonsoft.Json and Owin packages.
* Changed – Renamed HangFire.Autofac to Hangfire.Autofac.

2.2.0
* Added – Allow combining lifetime scope tags (by @chaoaretasty).

2.1.0
* Added – Ability to use non-tagged nested lifetime scopes.

2.0.1
* Changed – `AutofacJobActivator.LifetimeScopeTag` field is now public.