以下是在Asp.Net Core中实施长时间运行的后台工作的正确模式吗?或者我应该使用一些形式的
Task.Run
/TaskFactory.StartNew
与TaskCreationOptions.LongRunning
选项? public void Configure(IApplicationLifetime lifetime)
{
lifetime.ApplicationStarted.Register(() =>
{
// not awaiting the 'promise task' here
var t = DoWorkAsync(lifetime.ApplicationStopping);
lifetime.ApplicationStopped.Register(() =>
{
try
{
// give extra time to complete before shutting down
t.Wait(TimeSpan.FromSeconds(10));
}
catch (Exception)
{
// ignore
}
});
});
}
async Task DoWorkAsync(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
await // async method
}
}
Hangfire
这样的库来完成这个任务。 - NtFreX