Check also .NET Core 2.0 IHostedService
. Here is the documentation. From .NET Core 2.1 we will have BackgroundService
abstract class.
It can be used like this:
public class UpdateBackgroundService: BackgroundService
{
private readonly DbContext _context;
public UpdateTranslatesBackgroundService(DbContext context)
{
this._context= context;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await ...
}
}
In your startup you just have to register class:
public static IServiceProvider Build(IServiceCollection services)
{
//.....
services.AddSingleton<IHostedService, UpdateBackgroundService>();
services.AddTransient<IHostedService, UpdateBackgroundService>(); //For run at startup and die.
//.....
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…