NET核心的Hangfire依赖项注入

NET核心的Hangfire依赖项注入

本文介绍了NET核心的Hangfire依赖项注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Hangfire中使用.net核心的默认依赖项注入?

How can I use .net core's default dependency injection in Hangfire ?

我是Hangfire的新手,正在寻找一个适用于asp.net core的示例.

I am new to Hangfire and searching for an example which works with asp.net core.

推荐答案

请参阅GitHub上的完整示例 https://github.com/gonzigonz/HangfireCore-Example .
实时站点位于 http://hangfirecore.azurewebsites.net/

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

  1. 确保您拥有Hangfire的核心版本:
    dotnet add package Hangfire.AspNetCore

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

通过定义JobActivator来配置IoC.以下是与默认的asp.net核心容器服务一起使用的配置:

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);
    }
}

  • 下一步,在Startup.ConfigureServices方法中将hangfire注册为服务:

  • Next register hangfire as a service in the Startup.ConfigureServices method:

    services.AddHangfire(opt =>
        opt.UseSqlServerStorage("Your Hangfire Connection string"));
    

  • Startup.Configure方法中配置hangfire.关于您的问题,是将hangfire配置为使用我们上面刚刚定义的新HangfireActivator.为此,您必须为IServiceProvider提供hangfire,只需将其添加到Configure方法的参数列表中即可实现.在运行时,DI将为您提供此服务:

  • 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();
    }
    

  • 入队时,请使用注册类型,通常是您的界面.除非您以这种方式注册,否则不要使用具体类型.您必须使用在IoC中注册的类型,否则Hang​​fire找不到它.例如,例如,您已经注册了以下服务:

  • 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>();
    

  • 然后,您可以将DbManager与该类的实例化版本排队:

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

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

    但是,您不能对MyService做同样的事情.使用实例化版本入队将失败,因为DI仅会在注册接口时失败.在这种情况下,您将像这样入队:

    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());
    

    这篇关于NET核心的Hangfire依赖项注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-11 08:22