{"id":20893104,"url":"https://github.com/hangfireio/hangfire.autofac","last_synced_at":"2025-05-15T20:04:45.310Z","repository":{"id":15594195,"uuid":"18330189","full_name":"HangfireIO/Hangfire.Autofac","owner":"HangfireIO","description":"Hangfire job activator based on Autofac IoC container","archived":false,"fork":false,"pushed_at":"2025-01-10T06:45:57.000Z","size":12363,"stargazers_count":64,"open_issues_count":0,"forks_count":35,"subscribers_count":10,"default_branch":"main","last_synced_at":"2025-05-15T20:04:35.534Z","etag":null,"topics":["autofac","hangfire","integration","ioc"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/HangfireIO.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-04-01T13:25:27.000Z","updated_at":"2025-01-10T06:46:01.000Z","dependencies_parsed_at":"2024-01-31T04:49:26.244Z","dependency_job_id":"33de37c2-c8c8-4da6-a8ed-6cf3265958ba","html_url":"https://github.com/HangfireIO/Hangfire.Autofac","commit_stats":{"total_commits":88,"total_committers":10,"mean_commits":8.8,"dds":0.5113636363636364,"last_synced_commit":"4b9c10d520388ce1cac3576f93edbf52b60db83c"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HangfireIO%2FHangfire.Autofac","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HangfireIO%2FHangfire.Autofac/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HangfireIO%2FHangfire.Autofac/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HangfireIO%2FHangfire.Autofac/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/HangfireIO","download_url":"https://codeload.github.com/HangfireIO/Hangfire.Autofac/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254414499,"owners_count":22067272,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["autofac","hangfire","integration","ioc"],"created_at":"2024-11-18T10:14:41.433Z","updated_at":"2025-05-15T20:04:38.508Z","avatar_url":"https://github.com/HangfireIO.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# Hangfire.Autofac\n\n[![NuGet](https://img.shields.io/nuget/v/Hangfire.Autofac.svg)](https://www.nuget.org/packages/Hangfire.Autofac) [![Build status](https://ci.appveyor.com/api/projects/status/oncvxlqtnake9c86?svg=true)](https://ci.appveyor.com/project/hangfireio/hangfire-autofac) [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=HangfireIO_Hangfire.Autofac\u0026metric=alert_status)](https://sonarcloud.io/summary/new_code?id=HangfireIO_Hangfire.Autofac) [![Bugs](https://sonarcloud.io/api/project_badges/measure?project=HangfireIO_Hangfire.Autofac\u0026metric=bugs)](https://sonarcloud.io/summary/new_code?id=HangfireIO_Hangfire.Autofac) [![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=HangfireIO_Hangfire.Autofac\u0026metric=code_smells)](https://sonarcloud.io/summary/new_code?id=HangfireIO_Hangfire.Autofac) [![Coverage](https://sonarcloud.io/api/project_badges/measure?project=HangfireIO_Hangfire.Autofac\u0026metric=coverage)](https://sonarcloud.io/summary/new_code?id=HangfireIO_Hangfire.Autofac)\n\n[Autofac](https://autofac.org) integration for [Hangfire](https://www.hangfire.io). 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.\n\n*Hangfire.Autofac* resolves service instances using a child, tagged [lifetime scope](https://docs.autofac.org/en/latest/lifetime/index.html). 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**.\n\n## Installation\n\n*Hangfire.Autofac* is available as a NuGet Package. Type the following command into NuGet Package Manager Console window to install it:\n\n```\n\u003e dotnet add package Hangfire.Autofac\n```\n\n## Usage\n\nThe package provides an extension methods for the `IGlobalConfiguration` interface, so you can enable Autofac integration using the `GlobalConfiguration` class:\n\n```csharp\nvar builder = new ContainerBuilder();\n// builder.Register...\n\nGlobalConfiguration.Configuration.UseAutofacActivator(builder.Build());\n```\n\nAfter invoking the `UseAutofacActivator` method, Autofac-based implementation of the `JobActivator` class will be used to resolve job type instances during the background job processing.\n\n### Custom Scope Configuration\n\nStarting 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.\n\n```csharp\nGlobalConfiguration.Configuration.UseAutofacActivator(builder.Build(), (builder, context) =\u003e\n{\n    var tenantId = context.GetJobParameter\u003cstring\u003e(\"TenantId\", allowStale: true);\n    builder.Register\u003cMultiTenantDatabase\u003e(provider =\u003e new MultiTenantDatabase(tenantId));\n});\n```\n\n### Shared Components\n\nSometimes 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:\n\n```csharp\nbuilder.RegisterType\u003cDatabase\u003e().InstancePerBackgroundJob();\n```\n\n### Non-tagged scopes\n\nWhenever 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.\n\n```csharp\nvar builder = new ContainerBuilder();\n// builder.Register...\n\nGlobalConfiguration.Configuration.UseAutofacActivator(builder.Build(), false);\n```\n\nThen you can register services by using `InstancePerLifetimeScope` and expect them to work like intended.\n\n```csharp\nbuilder.RegisterType\u003cDatabase\u003e().InstancePerLifetimeScope();\n```\n\n### Deterministic Disposal\n\nThe *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*.\n\nFor example, the following components will be **disposed automatically**:\n\n```csharp\nbuilder.RegisterType\u003cSmsService\u003e();\nbuilder.RegisterType\u003cEmailService\u003e().InstancePerDependency();\nbuilder.RegisterType\u003cDatabase\u003e().InstancePerBackgroundJob();\n```\n\nAnd the following components **will not be disposed**:\n\n```csharp\nbuilder.RegisterType\u003cBackgroundJobClient\u003e().SingleInstance();\nbuilder.RegisterType\u003cMyService\u003e().ExternallyOwned();\n```\n\nPlease refer to the Autofac documentation to learn more about [Automatic Disposal](https://docs.autofac.org/en/latest/lifetime/disposal.html#automatic-disposal) feature.\n\n### Registering With Multiple Lifetime Scopes\n\nServices 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.\n\nTo register a service with both Autofac's PerRequest and Hangfire's PerBackgroundJob you could do any of the following:\n\nPassing Hangfire's scope tag to Autofac's `InstancePerHttpRequest`:\n```csharp\nbuilder.RegisterType\u003cSharedService\u003e().InstancePerHttpRequest(AutofacJobActivator.LifetimeScopeTag);\n```\n\nFrom Autofac 3.4.0 Autofac exposed their lifetime tag, `MatchingScopeLifetimeTags.RequestLifetimeScopeTag`, which can be used with `InstancePerBackgroundJob`:\n```csharp\nbuilder.RegisterType\u003cSharedService\u003e().InstancePerBackgroundJob(MatchingScopeLifetimeTags.RequestLifetimeScopeTag);\n```\n\nBoth scope tags can also be used directly with Autofac's `InstancePerMatchingLifetimeScope`\n```csharp\nvar requestTag = MatchingScopeLifetimeTags.RequestLifetimeScopeTag;\nvar jobTag = AutofacJobActivator.LifetimeScopeTag;\nbuilder.RegisterType\u003cSharedService\u003e().InstancePerMatchingLifetimeScope(requestTag, jobTag);\n```\n\n### Mixed Lifetime Scopes\n\nBeaware that if you are using multiple lifetime scopes to share services that all dependencies of those services need to be similarly registered. For example:\n\n```csharp\npublic class WebOnlyService(){ ... }\npublic class SharedService(WebOnlyService){ ... }\n\nbuilder.RegisterType\u003cSharedService\u003e().InstancePerRequest();\nbuilder.RegisterType\u003cSharedService\u003e().InstancePerMatchingLifetimeScope(requestTag, jobTag);\n```\n\nAttempting to resolve `SharedService` from a background job will throw an exception as Autofac will need to resolve `WebOnlyService` outside of a `RequestLifetimeScope`. \n\nAlso be aware that many web related properties that you may be using such as `HttpContext.Current` **will be unavailable**.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhangfireio%2Fhangfire.autofac","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhangfireio%2Fhangfire.autofac","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhangfireio%2Fhangfire.autofac/lists"}