Hangfire dependency injection with .net core

后端 未结 8 1914
孤城傲影
孤城傲影 2020-12-08 02:02

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.

8条回答
  •  旧时难觅i
    2020-12-08 02:23

    DoritoBandito's answer is incomplete or deprecated.

    public class EmailSender {
         public EmailSender(IDbContext dbContext, IEmailService emailService)
         {
             _dbContext = dbContext;
             _emailService = emailService;
         }
    }
    

    Register services:

    services.AddTransient();
    services.AddTransient();
    

    Enqueue:

    BackgroundJob.Enqueue(x => x.Send(13, "Hello!"));
    

    Source: http://docs.hangfire.io/en/latest/background-methods/passing-dependencies.html

提交回复
热议问题