diff --git a/docs/en/Background-Workers-Hangfire.md b/docs/en/Background-Workers-Hangfire.md index e1e6fc71ad..2ea7fd633e 100644 --- a/docs/en/Background-Workers-Hangfire.md +++ b/docs/en/Background-Workers-Hangfire.md @@ -66,6 +66,33 @@ public class MyLogWorker : HangfireBackgroundWorkerBase > You can directly implement the `IHangfireBackgroundWorker`, but `HangfireBackgroundWorkerBase` provides some useful properties like Logger. +### UnitOfWork + +For use with `UnitOfWorkAttribute`, you need to define an interface for worker: + +```csharp +public interface IMyLogWorker : IHangfireBackgroundWorker +{ +} + +[ExposeServices(typeof(IMyLogWorker))] +public class MyLogWorker : HangfireBackgroundWorkerBase, IMyLogWorker +{ + public MyLogWorker() + { + RecurringJobId = nameof(MyLogWorker); + CronExpression = Cron.Daily(); + } + + [UnitOfWork] + public override Task DoWorkAsync() + { + Logger.LogInformation("Executed MyLogWorker..!"); + return Task.CompletedTask; + } +} +``` + ## Register BackgroundWorkerManager After creating a background worker class, you should add it to the `IBackgroundWorkerManager`. The most common place is the `OnApplicationInitialization` method of your module class: @@ -78,6 +105,9 @@ public class MyModule : AbpModule ApplicationInitializationContext context) { context.AddBackgroundWorker(); + + //If the interface is defined + //context.AddBackgroundWorker(); } } ```` diff --git a/docs/zh-Hans/Background-Workers-Hangfire.md b/docs/zh-Hans/Background-Workers-Hangfire.md index bd63ff91e9..5e53928ca1 100644 --- a/docs/zh-Hans/Background-Workers-Hangfire.md +++ b/docs/zh-Hans/Background-Workers-Hangfire.md @@ -67,6 +67,33 @@ public class MyLogWorker : HangfireBackgroundWorkerBase > 你可以直接实现 `IHangfireBackgroundWorker`, 但是 `HangfireBackgroundWorkerBase` 提供了一些有用的属性,例如 `Logger`. +### UnitOfWork + +使用 `UnitOfWorkAttribute` 你需要为工作者定义一个接口: + +```csharp +public interface IMyLogWorker : IHangfireBackgroundWorker +{ +} + +[ExposeServices(typeof(IMyLogWorker))] +public class MyLogWorker : HangfireBackgroundWorkerBase, IMyLogWorker +{ + public MyLogWorker() + { + RecurringJobId = nameof(MyLogWorker); + CronExpression = Cron.Daily(); + } + + [UnitOfWork] + public override Task DoWorkAsync() + { + Logger.LogInformation("Executed MyLogWorker..!"); + return Task.CompletedTask; + } +} +``` + ## 注册到后台工作者管理器 创建一个后台工作者后, 你应该添加到 `IBackgroundWorkerManager`, 最常用的地方是在你模块类的 `OnApplicationInitialization` 方法中: @@ -79,6 +106,9 @@ public class MyModule : AbpModule ApplicationInitializationContext context) { context.AddBackgroundWorker(); + + //如果定义了接口 + //context.AddBackgroundWorker(); } } ````