.Net 核心托管服务保证完成
.Net core Hosted Services guaranteed to complete
我正在查看 .Net-Core 2.1 新功能 Hosted Services,
我看到它们的模型与 QueueBackgroundWorkItem
非常相似
队列后台工作项似乎有任务必须在 90 秒内执行的限制
The AppDomain shutdown can only be delayed 90 seconds (It’s actually the minimum of HttpRuntimeSection.ShutdownTimeout and processModel shutdownTimeLimit). If you have so many items queued that they can’t be completed in 90 seconds, the ASP.NET runtime will unload the AppDomain without waiting for the work items to finish.
托管服务是否有不同的行为,或者此限制是否仍然适用?
我担心如果我在我的托管服务上排队,如果它是一个非常长的 运行 任务,它是否仍能保证完成?
作为尝试正常关闭 Web 主机的一部分,结合配置的 ShutdownTimeout
构建取消令牌
var timeoutToken = new CancellationTokenSource(Options.ShutdownTimeout).Token;
if (!cancellationToken.CanBeCanceled)
{
cancellationToken = timeoutToken;
}
else
{
cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutToken).Token;
}
这成为停止托管服务时的关闭标记
// Fire the IHostedService.Stop
if (_hostedServiceExecutor != null)
{
await _hostedServiceExecutor.StopAsync(cancellationToken).ConfigureAwait(false);
}
在研究托管服务的潜在问题时,我从官方文档中发现了以下内容。
Deployment considerations and takeaways
It is important to note that the way you deploy your ASP.NET Core WebHost or .NET Core Host might impact the final solution. For instance, if you deploy your WebHost on IIS or a regular Azure App Service, your host can be shut down because of app pool recycles. But if you are deploying your host as a container into an orchestrator like Kubernetes or Service Fabric, you can control the assured number of live instances of your host. In addition, you could consider other approaches in the cloud especially made for these scenarios, like Azure Functions.
But even for a WebHost deployed into an app pool, there are scenarios like repopulating or flushing application’s in-memory cache, that would be still applicable.
The IHostedService interface provides a convenient way to start background tasks in an ASP.NET Core web application (in .NET Core 2.0) or in any process/host (starting in .NET Core 2.1 with IHost). Its main benefit is the opportunity you get with the graceful cancellation to clean-up code of your background tasks when the host itself is shutting down.
现在,基于您的顾虑,我认为不能保证您的 运行 长任务一定会完成,但它们可能有机会根据托管环境正常取消,如前所述在上面引用的声明中。
我正在查看 .Net-Core 2.1 新功能 Hosted Services, 我看到它们的模型与 QueueBackgroundWorkItem
非常相似队列后台工作项似乎有任务必须在 90 秒内执行的限制
The AppDomain shutdown can only be delayed 90 seconds (It’s actually the minimum of HttpRuntimeSection.ShutdownTimeout and processModel shutdownTimeLimit). If you have so many items queued that they can’t be completed in 90 seconds, the ASP.NET runtime will unload the AppDomain without waiting for the work items to finish.
托管服务是否有不同的行为,或者此限制是否仍然适用?
我担心如果我在我的托管服务上排队,如果它是一个非常长的 运行 任务,它是否仍能保证完成?
作为尝试正常关闭 Web 主机的一部分,结合配置的 ShutdownTimeout
var timeoutToken = new CancellationTokenSource(Options.ShutdownTimeout).Token;
if (!cancellationToken.CanBeCanceled)
{
cancellationToken = timeoutToken;
}
else
{
cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutToken).Token;
}
这成为停止托管服务时的关闭标记
// Fire the IHostedService.Stop
if (_hostedServiceExecutor != null)
{
await _hostedServiceExecutor.StopAsync(cancellationToken).ConfigureAwait(false);
}
在研究托管服务的潜在问题时,我从官方文档中发现了以下内容。
Deployment considerations and takeaways
It is important to note that the way you deploy your ASP.NET Core WebHost or .NET Core Host might impact the final solution. For instance, if you deploy your WebHost on IIS or a regular Azure App Service, your host can be shut down because of app pool recycles. But if you are deploying your host as a container into an orchestrator like Kubernetes or Service Fabric, you can control the assured number of live instances of your host. In addition, you could consider other approaches in the cloud especially made for these scenarios, like Azure Functions.
But even for a WebHost deployed into an app pool, there are scenarios like repopulating or flushing application’s in-memory cache, that would be still applicable.
The IHostedService interface provides a convenient way to start background tasks in an ASP.NET Core web application (in .NET Core 2.0) or in any process/host (starting in .NET Core 2.1 with IHost). Its main benefit is the opportunity you get with the graceful cancellation to clean-up code of your background tasks when the host itself is shutting down.
现在,基于您的顾虑,我认为不能保证您的 运行 长任务一定会完成,但它们可能有机会根据托管环境正常取消,如前所述在上面引用的声明中。