本文转载自微信公众号「后端Q」,作者conan。转载本文请联系后端Q公众号。
要求
Hangfire 适用于大多数 .NET 平台:.NET Framework 4.5 或更高版本、.NET Core 1.0 或更高版本,或任何与 .NET Standard 1.3 兼容的平台。您可以将它与几乎任何应用程序框架集成,包括 ASP.NET、ASP.NET Core、控制台应用程序、Windows 服务、WCF,以及社区驱动的框架,如 Nancy 或 ServiceStack。
存储
存储是 Hangfire 保存与后台作业处理相关的所有信息的地方。类型、方法名称、参数等所有细节都被序列化并放入存储中,没有数据保存在进程的内存中。存储子系统在 Hangfire 中被很好地抽象出来,可以为 RDBMS 和 NoSQL 解决方案实现。
这是您必须做出的主要决定,也是开始使用框架之前所需的唯一配置。以下示例显示如何使用 SQL Server 数据库配置 Hangfire。请注意,连接字符串可能会有所不同,具体取决于您的环境。
GlobalConfiguration.Configuration
.UseSqlServerStorage(@"Server=.\SQLEXPRESS; Database=Hangfire.Sample; Integrated Security=True");
- 1.
- 2.
客户端
Client 负责创建后台作业并将它们保存到 Storage 中。后台作业是一个应该在当前执行上下文之外执行的工作单元,例如在后台线程、其他进程中,甚至在不同的服务器上——这一切都可以通过 Hangfire 实现,即使没有额外的配置。
BackgroundJob.Enqueue(() => Console.WriteLine("Hello, world!"));
- 1.
请注意,这不是委托,而是表达式树。Hangfire 不是立即调用该方法,而是序列化类型 ( System.Console)、方法名称 ( WriteLine,带有所有参数类型以便稍后识别它) 和所有给定的参数,并将其放入 Storage。
服务器
Hangfire Server 通过查询存储来处理后台作业。粗略地说,它是一组后台线程,它们侦听 Storage 以获取新的后台作业,并通过反序列化类型、方法和参数来执行它们。
您可以将此后台作业服务器放置在您想要的任何进程中,包括像 ASP.NET 这样的危险进程——即使您终止了一个进程,您的后台作业也会在重新启动后自动重试。因此,在 Web 应用程序的基本配置中,您不再需要使用 Windows 服务进行后台处理。
using (new BackgroundJobServer())
{
Console.ReadLine();
}
- 1.
- 2.
- 3.
- 4.
安装
Hangfire 作为几个 NuGet 包分发,从主要的 Hangfire.Core 开始,它包含所有主要类和抽象。其他包如 Hangfire.SqlServer 提供功能或抽象实现。要开始使用 Hangfire,请安装主软件包并选择可用的存储空间之一。
Visual Studio 2017 发布后,出现了一种全新的 NuGet 包安装方式。所以我放弃了列出安装 NuGet 包的所有方法,并回退到使用该dotnet应用程序几乎在任何地方都可用的方法。
dotnet add package Hangfire.Core
dotnet add package Hangfire.SqlServer
- 1.
- 2.
配置
使用GlobalConfiguration类执行配置。它的Configuration属性提供了很多扩展方法,既有来自 Hangfire.Core 的,也有来自其他包的。如果你安装了一个新的包,不要犹豫,检查是否有新的扩展方法。
GlobalConfiguration.Configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage("Database=Hangfire.Sample; Integrated Security=True;", new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
UsePageLocksOnDequeue = true,
DisableGlobalLocks = true
})
.UseBatches()
.UsePerformanceCounters();
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
方法调用可以链接起来,因此不需要一次又一次地使用类名。全局配置是为了简单起见,几乎每个 Hangfire 类都允许您指定存储、过滤器等的覆盖。在 ASP.NET Core 环境中,全局配置类隐藏在AddHangfire方法中。
用法
以下是所有运行中的 Hangfire 组件,作为打印“Hello, world!”的完整工作示例。来自后台线程的消息。您可以注释与服务器相关的行,并多次运行该程序——只要您再次取消注释这些行,就会处理所有后台作业。
using System;
using Hangfire;
using Hangfire.SqlServer;
namespace ConsoleApplication2
{
class Program
{
static void Main()
{
GlobalConfiguration.Configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseColouredConsoleLogProvider()
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage("Database=Hangfire.Sample; Integrated Security=True;", new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
UsePageLocksOnDequeue = true,
DisableGlobalLocks = true
});
BackgroundJob.Enqueue(() => Console.WriteLine("Hello, world!"));
using (var server = new BackgroundJobServer())
{
Console.ReadLine();
}
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.