Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hangfire dependency injection with .NET Core

How can I use .NET Core's default dependency injection in Hangfire?

I am new to Hangfire and searching for an example which works with ASP.NET Core.

like image 739
eadam Avatar asked Jan 24 '17 13:01

eadam


People also ask

What is Hangfire in .NET core?

Hangfire Introduction and Implementation in .NET Core 6 Web API. Overview. Hangfire is open-source and used to schedule the job at a particular event and time. It is also used to create, process, and manage your background jobs. Basically, we use this in Background Processing without user intervention.

Can we use dependency injection in .NET framework?

NET supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. Dependency injection in . NET is a built-in part of the framework, along with configuration, logging, and the options pattern.


1 Answers

See full example on GitHub https://github.com/gonzigonz/HangfireCore-Example.
Live site at http://hangfirecore.azurewebsites.net/

  1. Make sure you have the Core version of Hangfire:
    dotnet add package Hangfire.AspNetCore

  2. Configure your IoC by defining a JobActivator. Below is the config for use with the default asp.net core container service:

    public class HangfireActivator : Hangfire.JobActivator {     private readonly IServiceProvider _serviceProvider;      public HangfireActivator(IServiceProvider serviceProvider)     {         _serviceProvider = serviceProvider;     }      public override object ActivateJob(Type type)     {         return _serviceProvider.GetService(type);     } }   
  3. Next register hangfire as a service in the Startup.ConfigureServices method:

    services.AddHangfire(opt =>      opt.UseSqlServerStorage("Your Hangfire Connection string")); 
  4. Configure hangfire in the Startup.Configure method. In relationship to your question, the key is to configure hangfire to use the new HangfireActivator we just defined above. To do so you will have to provide hangfire with the IServiceProvider and this can be achieved by just adding it to the list of parameters for the Configure method. At runtime, DI will providing this service for you:

    public void Configure(     IApplicationBuilder app,      IHostingEnvironment env,      ILoggerFactory loggerFactory,     IServiceProvider serviceProvider) {     ...      // Configure hangfire to use the new JobActivator we defined.     GlobalConfiguration.Configuration         .UseActivator(new HangfireActivator(serviceProvider));      // The rest of the hangfire config as usual.     app.UseHangfireServer();     app.UseHangfireDashboard(); }   
  5. When you enqueue a job, use the registered type which usually is your interface. Don't use a concrete type unless you registered it that way. You must use the type registered with your IoC else Hangfire won't find it. For Example say you've registered the following services:

    services.AddScoped<DbManager>(); services.AddScoped<IMyService, MyService>(); 

Then you could enqueue DbManager with an instantiated version of the class:

    BackgroundJob.Enqueue(() => dbManager.DoSomething()); 

However you could not do the same with MyService. Enqueuing with an instantiated version would fail because DI would fail as only the interface is registered. In this case you would enqueue like this:

    BackgroundJob.Enqueue<IMyService>( ms => ms.DoSomething()); 
like image 66
Gonzalo Lucero Avatar answered Sep 23 '22 17:09

Gonzalo Lucero