Skip to content
This repository has been archived by the owner on Nov 20, 2022. It is now read-only.
/ Hangfire.Unity4 Public archive

Unity v4x job activator for Hangfire

License

Notifications You must be signed in to change notification settings

Hameds/Hangfire.Unity4

Repository files navigation

Hangfire.Unity4

Build status NuGet

This project is based on the Hangfire.Unity project with 2 chnages

  • Hangfire.Unity works fine for Unity v5+ but due to namespace changes you can't use it for Unity v4. This package (Hangfire.Unity4) works with old namespace of unity which is Microsoft.Practices.Unity
  • This package also have a helper class for lifetime management called PerRequestOrHierarchicalLifeTimeManager which if you don't have HttpContext available, it will offer HierarchicalLifetimeManager instead of PerRequestLifetimeManager you can use this to in RegisterTypes method to avoid Resolve exception
public static void RegisterTypes(IUnityContainer container)
{
    container.RegisterType<IMyService, MyService>(new PerRequestOrHierarchicalLifeTimeManager());
}

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.Unity4 is available as a NuGet Package. Type the following command into NuGet Package Manager Console window to install it:

Install-Package Hangfire.Unity4

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 v4x job activator for Hangfire

Topics

Resources

License

Stars

Watchers

Forks

Languages