diff --git a/docs/en/Background-Workers-Hangfire.md b/docs/en/Background-Workers-Hangfire.md index b076cf0395..5a456b1092 100644 --- a/docs/en/Background-Workers-Hangfire.md +++ b/docs/en/Background-Workers-Hangfire.md @@ -53,7 +53,7 @@ public class MyLogWorker : HangfireBackgroundWorkerBase CronExpression = Cron.Daily(); } - public override Task DoWorkAsync() + public override Task DoWorkAsync(CancellationToken cancellationToken = default) { Logger.LogInformation("Executed MyLogWorker..!"); return Task.CompletedTask; @@ -68,14 +68,7 @@ public class MyLogWorker : HangfireBackgroundWorkerBase ### 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() @@ -84,11 +77,13 @@ public class MyLogWorker : HangfireBackgroundWorkerBase, IMyLogWorker CronExpression = Cron.Daily(); } - [UnitOfWork] - public override Task DoWorkAsync() + public override Task DoWorkAsync(CancellationToken cancellationToken = default) { - Logger.LogInformation("Executed MyLogWorker..!"); - return Task.CompletedTask; + using (var uow = LazyServiceProvider.LazyGetRequiredService().Begin()) + { + Logger.LogInformation("Executed MyLogWorker..!"); + return Task.CompletedTask; + } } } ``` @@ -105,9 +100,6 @@ public class MyModule : AbpModule ApplicationInitializationContext context) { await context.AddBackgroundWorkerAsync(); - - //If the interface is defined - //await context.AddBackgroundWorkerAsync(); } } ```` diff --git a/docs/zh-Hans/Background-Workers-Hangfire.md b/docs/zh-Hans/Background-Workers-Hangfire.md index b02a793d3f..fde7b35973 100644 --- a/docs/zh-Hans/Background-Workers-Hangfire.md +++ b/docs/zh-Hans/Background-Workers-Hangfire.md @@ -54,7 +54,7 @@ public class MyLogWorker : HangfireBackgroundWorkerBase CronExpression = Cron.Daily(); } - public override Task DoWorkAsync() + public override Task DoWorkAsync(CancellationToken cancellationToken = default) { Logger.LogInformation("Executed MyLogWorker..!"); return Task.CompletedTask; @@ -69,14 +69,7 @@ public class MyLogWorker : HangfireBackgroundWorkerBase ### UnitOfWork -使用 `UnitOfWorkAttribute` 你需要为工作者定义一个接口: - ```csharp -public interface IMyLogWorker : IHangfireBackgroundWorker -{ -} - -[ExposeServices(typeof(IMyLogWorker))] public class MyLogWorker : HangfireBackgroundWorkerBase, IMyLogWorker { public MyLogWorker() @@ -85,11 +78,13 @@ public class MyLogWorker : HangfireBackgroundWorkerBase, IMyLogWorker CronExpression = Cron.Daily(); } - [UnitOfWork] - public override Task DoWorkAsync() + public override Task DoWorkAsync(CancellationToken cancellationToken = default) { - Logger.LogInformation("Executed MyLogWorker..!"); - return Task.CompletedTask; + using (var uow = LazyServiceProvider.LazyGetRequiredService().Begin()) + { + Logger.LogInformation("Executed MyLogWorker..!"); + return Task.CompletedTask; + } } } ``` @@ -106,9 +101,6 @@ public class MyModule : AbpModule ApplicationInitializationContext context) { await context.AddBackgroundWorkerAsync(); - - //如果定义了接口 - //await context.AddBackgroundWorkerAsync(); } } ```` @@ -127,4 +119,4 @@ context.ServiceProvider 它解析给定的后台工作者并添加到 `IBackgroundWorkerManager`. -虽然我们通常在 `OnApplicationInitializationAsync` 中添加后台工作者, 但对此没有限制. 你可以在任何地方注入 `IBackgroundWorkerManager` 并在运行时添加后台工作者. \ No newline at end of file +虽然我们通常在 `OnApplicationInitializationAsync` 中添加后台工作者, 但对此没有限制. 你可以在任何地方注入 `IBackgroundWorkerManager` 并在运行时添加后台工作者.