![]() |
VOOZH | about |
dotnet add package Hangfire.Autofac --version 2.7.0
NuGet\Install-Package Hangfire.Autofac -Version 2.7.0
<PackageReference Include="Hangfire.Autofac" Version="2.7.0" />
<PackageVersion Include="Hangfire.Autofac" Version="2.7.0" />Directory.Packages.props
<PackageReference Include="Hangfire.Autofac" />Project file
paket add Hangfire.Autofac --version 2.7.0
#r "nuget: Hangfire.Autofac, 2.7.0"
#:package Hangfire.Autofac@2.7.0
#addin nuget:?package=Hangfire.Autofac&version=2.7.0Install as a Cake Addin
#tool nuget:?package=Hangfire.Autofac&version=2.7.0Install as a Cake Tool
👁 NuGet
👁 Build status
👁 Quality Gate Status
👁 Bugs
👁 Code Smells
👁 Coverage
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.
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
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.
Starting from version 2.7.0 it is possible to define a custom scope configuration action and pass background job-related data to the service registration logic, like identifier, parameters and so on.
GlobalConfiguration.Configuration.UseAutofacActivator(builder.Build(), (builder, context) =>
{
var tenantId = context.GetJobParameter<string>("TenantId", allowStale: true);
builder.Register<MultiTenantDatabase>(provider => new MultiTenantDatabase(tenantId));
});
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();
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();
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.
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);
Beaware that if you are using multiple lifetime scopes to share services that all dependencies of those services need to be similarly registered. 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 | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp1.0 netcoreapp1.0 was computed. netcoreapp1.1 netcoreapp1.1 was computed. netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard1.3 netstandard1.3 is compatible. netstandard1.4 netstandard1.4 was computed. netstandard1.5 netstandard1.5 was computed. netstandard1.6 netstandard1.6 was computed. netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net45 net45 is compatible. net451 net451 was computed. net452 net452 was computed. net46 net46 was computed. net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen30 tizen30 was computed. tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Universal Windows Platform | uap uap was computed. uap10.0 uap10.0 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
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 |
Showing the top 2 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。
|
|
| sd797994/Oxygen-Dapr.EshopSample |
| Version | Downloads | Last Updated |
|---|---|---|
| 2.7.0 | 912,527 | 1/9/2025 |
| 2.6.0 | 1,141,871 | 2/16/2024 |
| 2.5.0 | 69,390 | 1/23/2024 |
| 2.4.1 | 653,598 | 5/25/2023 |
| 2.4.0 | 3,106 | 5/25/2023 |
| 2.3.1 | 7,503,700 | 6/8/2017 |
| 2.3.0 | 5,598 | 6/7/2017 |
| 2.2.0 | 491,575 | 2/24/2016 |
| 2.1.0 | 19,068 | 12/16/2015 |
| 2.0.1 | 59,056 | 10/15/2015 |
| 2.0.0 | 26,948 | 10/1/2015 |
| 2.0.0-beta1 | 3,298 | 9/3/2015 |
| 1.2.0 | 3,330 | 10/1/2015 |
| 1.2.0-beta2 | 1,977 | 9/3/2015 |
| 1.2.0-beta1 | 2,352 | 7/22/2015 |
| 1.1.0 | 27,756 | 4/13/2015 |
| 1.0.0 | 12,873 | 6/30/2014 |
| 0.1.2 | 5,682 | 5/2/2014 |
https://github.com/HangfireIO/Hangfire.Autofac/releases
2.7.0
* Added – Support for custom scope configuration logic with background job context data.
* Project – Faster continuous integration builds with the modern Powershell.
* Project – Don't emit warnings when building with .NET 9.0 SDK.
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.