|
|
@ -54,7 +54,7 @@ public class MyLogWorker : HangfireBackgroundWorkerBase |
|
|
CronExpression = Cron.Daily(); |
|
|
CronExpression = Cron.Daily(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public override Task DoWorkAsync() |
|
|
public override Task DoWorkAsync(CancellationToken cancellationToken = default) |
|
|
{ |
|
|
{ |
|
|
Logger.LogInformation("Executed MyLogWorker..!"); |
|
|
Logger.LogInformation("Executed MyLogWorker..!"); |
|
|
return Task.CompletedTask; |
|
|
return Task.CompletedTask; |
|
|
@ -69,14 +69,7 @@ public class MyLogWorker : HangfireBackgroundWorkerBase |
|
|
|
|
|
|
|
|
### UnitOfWork |
|
|
### UnitOfWork |
|
|
|
|
|
|
|
|
使用 `UnitOfWorkAttribute` 你需要为工作者定义一个接口: |
|
|
|
|
|
|
|
|
|
|
|
```csharp |
|
|
```csharp |
|
|
public interface IMyLogWorker : IHangfireBackgroundWorker |
|
|
|
|
|
{ |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[ExposeServices(typeof(IMyLogWorker))] |
|
|
|
|
|
public class MyLogWorker : HangfireBackgroundWorkerBase, IMyLogWorker |
|
|
public class MyLogWorker : HangfireBackgroundWorkerBase, IMyLogWorker |
|
|
{ |
|
|
{ |
|
|
public MyLogWorker() |
|
|
public MyLogWorker() |
|
|
@ -85,11 +78,13 @@ public class MyLogWorker : HangfireBackgroundWorkerBase, IMyLogWorker |
|
|
CronExpression = Cron.Daily(); |
|
|
CronExpression = Cron.Daily(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
[UnitOfWork] |
|
|
public override Task DoWorkAsync(CancellationToken cancellationToken = default) |
|
|
public override Task DoWorkAsync() |
|
|
|
|
|
{ |
|
|
{ |
|
|
Logger.LogInformation("Executed MyLogWorker..!"); |
|
|
using (var uow = LazyServiceProvider.LazyGetRequiredService<IUnitOfWorkManager>().Begin()) |
|
|
return Task.CompletedTask; |
|
|
{ |
|
|
|
|
|
Logger.LogInformation("Executed MyLogWorker..!"); |
|
|
|
|
|
return Task.CompletedTask; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
``` |
|
|
``` |
|
|
@ -106,9 +101,6 @@ public class MyModule : AbpModule |
|
|
ApplicationInitializationContext context) |
|
|
ApplicationInitializationContext context) |
|
|
{ |
|
|
{ |
|
|
await context.AddBackgroundWorkerAsync<MyLogWorker>(); |
|
|
await context.AddBackgroundWorkerAsync<MyLogWorker>(); |
|
|
|
|
|
|
|
|
//如果定义了接口 |
|
|
|
|
|
//await context.AddBackgroundWorkerAsync<IMyLogWorker>(); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
```` |
|
|
```` |
|
|
@ -127,4 +119,4 @@ context.ServiceProvider |
|
|
|
|
|
|
|
|
它解析给定的后台工作者并添加到 `IBackgroundWorkerManager`. |
|
|
它解析给定的后台工作者并添加到 `IBackgroundWorkerManager`. |
|
|
|
|
|
|
|
|
虽然我们通常在 `OnApplicationInitializationAsync` 中添加后台工作者, 但对此没有限制. 你可以在任何地方注入 `IBackgroundWorkerManager` 并在运行时添加后台工作者. |
|
|
虽然我们通常在 `OnApplicationInitializationAsync` 中添加后台工作者, 但对此没有限制. 你可以在任何地方注入 `IBackgroundWorkerManager` 并在运行时添加后台工作者. |
|
|
|