.Net核心托管服务保证将完成 [英] .Net core Hosted Services guaranteed to complete

查看:110
本文介绍了.Net核心托管服务保证将完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看.Net-Core 2.1的新功能托管服务
我看到它们的建模非常类似于 QueueBackgroundWorkItem

I'm looking at .Net-Core 2.1 new feature Hosted Services, I see that they are very similarly modelled to QueueBackgroundWorkItem

队列后台工作项似乎有一个限制,即该任务必须在90秒内执行

The Queue Background work item seems to have a limitation that the task must execute within 90 seconds


AppDomain关闭只能是延迟90秒(这实际上是HttpRuntimeSection.ShutdownTimeout和processModel shutdownTimeLimit的最小值)。如果您排队的项目太多,无法在90秒内完成,则ASP.NET运行时将卸载AppDomain,而无需等待工作项目完成。

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.

托管服务是否有其他行为,还是仍然存在此限制?

Do hosted services have different behaviors or does this limitation still apply?

我担心是否在托管服务上排队,如果这是一项长期运行的任务,是否还能保证完成?

I'm worried that if I queue something on my hosted service, if it's a really long running task, is it still guaranteed to complete?

推荐答案

作为尝试正常关闭Web主机结合配置的 ShutdownTimeout

As part of trying to gracefully shut down the web host builds a cancellation token in combination with the configured ShutdownTimeout

var timeoutToken = new CancellationTokenSource(Options.ShutdownTimeout).Token;
if (!cancellationToken.CanBeCanceled)
{
    cancellationToken = timeoutToken;
}
else
{
    cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutToken).Token;
}

来源

停止托管服务时关闭令牌

This becomes the shut down token when stopping hosted services

// Fire the IHostedService.Stop
if (_hostedServiceExecutor != null)
{
    await _hostedServiceExecutor.StopAsync(cancellationToken).ConfigureAwait(false);
}

在研究托管服务的潜在问题时,我从官方文档中发现了以下内容

In researching the potential issues with hosted services I came across the following from official documentation.

部署注意事项和收获


请务必注意,部署ASP.NET Core WebHost或.NET Core Host的方式可能会影响最终解决方案。例如,如果您将WebHost部署在IIS或常规的Azure App Service上,则由于应用程序池回收,主机可能会被关闭。但是,如果要将主机作为容器部署到诸如Kubernetes或Service Fabric的编排器中,则可以控制主机的活动实例的保证数量。此外,您可以考虑其他专门针对这些方案的云方法,例如Azure Functions。

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.

但是,即使对于部署到应用程序池中的WebHost,也存在方案

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.

IHostedService接口提供了一种方便的方法来启动ASP.NET Core Web应用程序中的后台任务,例如重新填充或刷新应用程序的内存中缓存。 (在.NET Core 2.0中)或任何进程/主机中(从具有IHost的.NET Core 2.1开始)。它的主要好处是,当主机本身关闭时,可以通过优雅的取消获得清理后台任务代码的机会。

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.

现在,基于您的担心,我现在可以保证不能保证您长期运行的任务将完成,但是可以根据托管环境(如上面引用的声明中所述)有选择地取消它们。

Now from this based on your concerns I would gather that there is no guarantee that your long running tasks will complete, but they may be given the opportunity for a graceful cancellation based on the hosting environment as explained in the quoted statement above.

这篇关于.Net核心托管服务保证将完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆