All Projects → HangfireIO → Hangfire.autofac

HangfireIO / Hangfire.autofac

Licence: mit
Hangfire job activator based on Autofac IoC container

Projects that are alternatives of or similar to Hangfire.autofac

Sitewhere
SiteWhere is an industrial strength open-source application enablement platform for the Internet of Things (IoT). It provides a multi-tenant microservice-based infrastructure that includes device/asset management, data ingestion, big-data storage, and integration through a modern, scalable architecture. SiteWhere provides REST APIs for all system functionality. SiteWhere provides SDKs for many common device platforms including Android, iOS, Arduino, and any Java-capable platform such as Raspberry Pi rapidly accelerating the speed of innovation.
Stars: ✭ 788 (+1359.26%)
Mutual labels:  integration
Furion
Make .NET development easier, more versatile, and more popular.
Stars: ✭ 902 (+1570.37%)
Mutual labels:  ioc
Zope.sqlalchemy
Integration of SQLAlchemy with transaction management
Stars: ✭ 30 (-44.44%)
Mutual labels:  integration
Di
Dependency Injection and IoC framework for PHP
Stars: ✭ 5 (-90.74%)
Mutual labels:  ioc
Snow Grafana Proxy
Connector for grafana simple-json data source and ServiceNow incidents retieved over ServiceNow API
Stars: ✭ 18 (-66.67%)
Mutual labels:  integration
Walkoff
A flexible, easy to use, automation framework allowing users to integrate their capabilities and devices to cut through the repetitive, tedious tasks slowing them down. #nsacyber
Stars: ✭ 855 (+1483.33%)
Mutual labels:  integration
Splat
Makes things cross-platform
Stars: ✭ 753 (+1294.44%)
Mutual labels:  ioc
Reviewbot
Reviewbot is a Slack bot that shows reviewable pull requests.
Stars: ✭ 40 (-25.93%)
Mutual labels:  integration
Intellij jahia plugin
Jahia's definitions.cnd files syntax highlighting, code completion, and other amazing stuff
Stars: ✭ 19 (-64.81%)
Mutual labels:  integration
Virustotal Tools
Submits multiple domains to VirusTotal API
Stars: ✭ 29 (-46.3%)
Mutual labels:  ioc
Sparkling Water
Sparkling Water provides H2O functionality inside Spark cluster
Stars: ✭ 887 (+1542.59%)
Mutual labels:  integration
Slack Gitlab Notifier
Gitlab-Slack Notifier - Integration between Gitlab Webhook Events and Slack Bot notifications
Stars: ✭ 17 (-68.52%)
Mutual labels:  integration
P0f3 Api Py
Python client for p0f3 API
Stars: ✭ 12 (-77.78%)
Mutual labels:  integration
Shiftscheduler
A boilerplate ASP.NET Core project, including a sample employee shift scheduler app
Stars: ✭ 5 (-90.74%)
Mutual labels:  ioc
Malware Ioc
Indicators of Compromises (IOC) of our various investigations
Stars: ✭ 955 (+1668.52%)
Mutual labels:  ioc
Webhook
webhook is a lightweight incoming webhook server to run shell commands
Stars: ✭ 7,201 (+13235.19%)
Mutual labels:  integration
Figmiro Plugin
Figma Integration with Miro (Plugin)
Stars: ✭ 23 (-57.41%)
Mutual labels:  integration
Grav Plugin Tinymce Editor
TinyMCE Editor Integration Plugin for Grav
Stars: ✭ 44 (-18.52%)
Mutual labels:  integration
Inversifyjs
InversifyJS is a lightweight inversion of control (IoC) container for TypeScript and JavaScript apps. An IoC container uses a class constructor to identify and inject its dependencies. InversifyJS has a friendly API and encourages the usage of the best OOP and IoC practices.
Stars: ✭ 8,399 (+15453.7%)
Mutual labels:  ioc
Bigchaindb Hyperledger
BigchainDB integration with HyperLedger Fabric. In collaboration with TheLedger
Stars: ✭ 20 (-62.96%)
Mutual labels:  integration

Hangfire.Autofac Build status Travis

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:

Install-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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].