我正在使用在 this blog post 中找到的代码在我的 ASP.NET Core 网站中实现定时后台服务。作为模特。执行实际工作的位如下所示......
protected async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
// Put your code here
// Wait 30 minutes before running again.
await Task.Delay(TimeSpan.FromMinutes(30), stoppingToken);
}
}
这工作正常,直到某些原因导致服务崩溃。显然,在生产应用程序中,您希望这不会发生,但实际上,我需要考虑这种可能性。
据我所知,框架创建并启动了任务,应用程序的其余部分对此一无所知。我可以检测
IHostedService
服务崩溃,我可以重新启动它吗?如果没有,我该怎么办?定期运行此程序对网站至关重要,因此我需要知道我可以处理任何问题。
最佳答案
If not, what do I do? Having this run at regular intervals is crucial to the web site, so I need to know I can handle any problems.
您应该在单独的独立进程中运行关键的后台服务。这可以是 Azure Functions 或 Docker,甚至是启用了自动重启的 Win32 服务。这样,其他东西会根据需要重新启动您的流程。
关于c# - ASP.NET Core - 如何检测 IHostedService 是否在运行时崩溃?我可以重新启动它吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56314506/