From e460121b3d08ab641752adaa558625934b29bdb3 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Mon, 27 Mar 2023 11:07:20 +0800 Subject: [PATCH 1/2] Update Hangfire document --- docs/en/Background-Jobs-Hangfire.md | 2 ++ docs/en/Background-Workers-Hangfire.md | 2 ++ docs/zh-Hans/Background-Jobs-Hangfire.md | 2 ++ docs/zh-Hans/Background-Workers-Hangfire.md | 2 ++ 4 files changed, 8 insertions(+) diff --git a/docs/en/Background-Jobs-Hangfire.md b/docs/en/Background-Jobs-Hangfire.md index c6c3d69ad7..ca2067980e 100644 --- a/docs/en/Background-Jobs-Hangfire.md +++ b/docs/en/Background-Jobs-Hangfire.md @@ -66,6 +66,8 @@ After you have installed these NuGet packages, you need to configure your projec } ```` +> You have to configure a storage for Hangfire. + 2. If you want to use hangfire's dashboard, you can add `UseHangfireDashboard` call in the `OnApplicationInitialization` method in `Module` class ````csharp diff --git a/docs/en/Background-Workers-Hangfire.md b/docs/en/Background-Workers-Hangfire.md index 5a456b1092..2136e3afb3 100644 --- a/docs/en/Background-Workers-Hangfire.md +++ b/docs/en/Background-Workers-Hangfire.md @@ -40,6 +40,8 @@ public class YourModule : AbpModule > Hangfire background worker integration provides an adapter `HangfirePeriodicBackgroundWorkerAdapter` to automatically load any `PeriodicBackgroundWorkerBase` and `AsyncPeriodicBackgroundWorkerBase` derived classes as `IHangfireBackgroundWorker` instances. This allows you to still to easily switch over to use Hangfire as the background manager even you have existing background workers that are based on the [default background workers implementation](Background-Workers.md). +> You have to configure a storage for Hangfire. + ## Create a Background Worker `HangfireBackgroundWorkerBase` is an easy way to create a background worker. diff --git a/docs/zh-Hans/Background-Jobs-Hangfire.md b/docs/zh-Hans/Background-Jobs-Hangfire.md index fb899758cc..d3fb341bab 100644 --- a/docs/zh-Hans/Background-Jobs-Hangfire.md +++ b/docs/zh-Hans/Background-Jobs-Hangfire.md @@ -66,6 +66,8 @@ public class YourModule : AbpModule } ```` +> 你必须为Hangfire配置一个存储 + ### 指定队列 你可以使用 [`QueueAttribute`](https://docs.hangfire.io/en/latest/background-processing/configuring-queues.html) 来指定队列. diff --git a/docs/zh-Hans/Background-Workers-Hangfire.md b/docs/zh-Hans/Background-Workers-Hangfire.md index fde7b35973..b7e1aee900 100644 --- a/docs/zh-Hans/Background-Workers-Hangfire.md +++ b/docs/zh-Hans/Background-Workers-Hangfire.md @@ -41,6 +41,8 @@ public class YourModule : AbpModule > Hangfire后台工作者集成提供了 `HangfirePeriodicBackgroundWorkerAdapter` 来适配 `PeriodicBackgroundWorkerBase` 和 `AsyncPeriodicBackgroundWorkerBase` 派生类. 所以你依然可以按照[后台工作者文档](Background-Workers.md)来定义后台作业. +> 你必须为Hangfire配置一个存储 + ## 创建后台工作者 `HangfireBackgroundWorkerBase` 是创建一个后台工作者简单的方法. From 8751ecb86939c7248bac9553fd3ac215086ab12d Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Mon, 27 Mar 2023 11:16:33 +0800 Subject: [PATCH 2/2] Update document --- docs/en/Background-Workers-Hangfire.md | 42 +++++++++++++++++++++ docs/zh-Hans/Background-Workers-Hangfire.md | 28 ++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/docs/en/Background-Workers-Hangfire.md b/docs/en/Background-Workers-Hangfire.md index 2136e3afb3..9506eb97a7 100644 --- a/docs/en/Background-Workers-Hangfire.md +++ b/docs/en/Background-Workers-Hangfire.md @@ -40,8 +40,50 @@ public class YourModule : AbpModule > Hangfire background worker integration provides an adapter `HangfirePeriodicBackgroundWorkerAdapter` to automatically load any `PeriodicBackgroundWorkerBase` and `AsyncPeriodicBackgroundWorkerBase` derived classes as `IHangfireBackgroundWorker` instances. This allows you to still to easily switch over to use Hangfire as the background manager even you have existing background workers that are based on the [default background workers implementation](Background-Workers.md). +## Configuration + +You can install any storage for Hangfire. The most common one is SQL Server (see the [Hangfire.SqlServer](https://www.nuget.org/packages/Hangfire.SqlServer) NuGet package). + +After you have installed these NuGet packages, you need to configure your project to use Hangfire. + +1.First, we change the `Module` class (example: `HttpApiHostModule`) to add Hangfire configuration of the storage and connection string in the `ConfigureServices` method: + +````csharp + public override void ConfigureServices(ServiceConfigurationContext context) + { + var configuration = context.Services.GetConfiguration(); + var hostingEnvironment = context.Services.GetHostingEnvironment(); + + //... other configarations. + + ConfigureHangfire(context, configuration); + } + + private void ConfigureHangfire(ServiceConfigurationContext context, IConfiguration configuration) + { + context.Services.AddHangfire(config => + { + config.UseSqlServerStorage(configuration.GetConnectionString("Default")); + }); + } +```` + > You have to configure a storage for Hangfire. +2. If you want to use hangfire's dashboard, you can add `UseHangfireDashboard` call in the `OnApplicationInitialization` method in `Module` class + +````csharp + public override void OnApplicationInitialization(ApplicationInitializationContext context) + { + var app = context.GetApplicationBuilder(); + + // ... others + + app.UseHangfireDashboard(); //should add to the request pipeline before the app.UseConfiguredEndpoints() + app.UseConfiguredEndpoints(); + } +```` + ## Create a Background Worker `HangfireBackgroundWorkerBase` is an easy way to create a background worker. diff --git a/docs/zh-Hans/Background-Workers-Hangfire.md b/docs/zh-Hans/Background-Workers-Hangfire.md index b7e1aee900..bdd546eba8 100644 --- a/docs/zh-Hans/Background-Workers-Hangfire.md +++ b/docs/zh-Hans/Background-Workers-Hangfire.md @@ -41,6 +41,34 @@ public class YourModule : AbpModule > Hangfire后台工作者集成提供了 `HangfirePeriodicBackgroundWorkerAdapter` 来适配 `PeriodicBackgroundWorkerBase` 和 `AsyncPeriodicBackgroundWorkerBase` 派生类. 所以你依然可以按照[后台工作者文档](Background-Workers.md)来定义后台作业. +## 配置 + +你可以安装任何Hangfire存储. 最常用的是SQL Server(参阅[Hangfire.SqlServer](https://www.nuget.org/packages/Hangfire.SqlServer)NuGet包). + +当你安装NuGet包后,你需要为你的项目配置Hangfire. + +1.首先, 我们需要更改 `Module` 类 (例如: `HttpApiHostModule`) 的 `ConfigureServices` 方法去配置Hangfire存储和连接字符串: + +````csharp + public override void ConfigureServices(ServiceConfigurationContext context) + { + var configuration = context.Services.GetConfiguration(); + var hostingEnvironment = context.Services.GetHostingEnvironment(); + + //... other configarations. + + ConfigureHangfire(context, configuration); + } + + private void ConfigureHangfire(ServiceConfigurationContext context, IConfiguration configuration) + { + context.Services.AddHangfire(config => + { + config.UseSqlServerStorage(configuration.GetConnectionString("Default")); + }); + } +```` + > 你必须为Hangfire配置一个存储 ## 创建后台工作者