xavero / Hangfire.Unity

Unity job activator for Hangfire

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hangfire.Unity

This project is based on the Hangfire.Ninject project.

Hangfire background job activator based on Unity IoC Container. It allows you to use instance methods of classes that define parametrized constructors:

public class EmailService
{
	private DbContext _context;
    private IEmailSender _sender;
	
	public EmailService(DbContext context, IEmailSender sender)
	{
		_context = context;
		_sender = sender;
	}
	
	public void Send(int userId, string message)
	{
		var user = _context.Users.Get(userId);
		_sender.Send(user.Email, message);
	}
}	

// Somewhere in the code
BackgroundJob.Enqueue<EmailService>(x => x.Send(1, "Hello, world!"));

Improve the testability of your jobs without static factories!

Installation

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

Install-Package Hangfire.Unity

Usage

The package provides an extension method for OWIN bootstrapper:

app.UseHangfire(config =>
{
    var container = new UnityContainer();
	// Setup your unity container
	
	// Use it with Hangfire
    config.UseUnityActivator(container);
});

In order to use the library outside of web application, set the static JobActivator.Current property:

var container = new UnityContainer();
JobActivator.Current = new UnityJobActivator(container);

About

Unity job activator for Hangfire

License:Other


Languages

Language:C# 100.0%