1. 首页
  2. 其他

.NET Core 的 Hangfire 依赖注入

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

问题描述

如何在 Hangfire 中使用 .NET Core 的默认依赖注入?

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 添加包 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 中注册的类型,否则 Hangfire 将找不到它.例如假设您注册了以下服务:

  • 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 Core 的 Hangfire 依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

    .NET Core 的 Hangfire 依赖注入 为WP2原创文章,链接:https://www.wp2.cn/other/net-core-%e7%9a%84-hangfire-%e4%be%9d%e8%b5%96%e6%b3%a8%e5%85%a5/