IHostedService 无故停止 [英] IHostedService Stop without any reason

查看:41
本文介绍了IHostedService 无故停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能向我解释为什么我的服务器无缘无故停止了?在我的 IHostedService 实现下面:

Could anyone explain to me why my server stopped for no reason? below my IHostedService implementation:

public class HostServiceBox : IHostedService
    {
        public Task StartAsync(CancellationToken cancellationToken)
        {
            return Task.Run(() =>
            {
                DomonutyBoxBusiness.StartBoxListening(); //STARTUP Box listening

                while (true)
                {
                    Logging.Info(DateTime.Now + " HostServiceBox Running");
                    Thread.Sleep(10000);
                }
            }, cancellationToken);
        }

        public Task StopAsync(CancellationToken cancellationToken)
        {
            Logging.Info(DateTime.Now + " StopAsync");

            //TODO impplement a Stop litening all boxes
            throw new NotImplementedException();
        }
    }

这是我的日志?

    .....
2/24/2018 8:31:27 PM HostServiceBox Running
2/24/2018 8:32:27 PM HostServiceBox Running
2/24/2018 8:33:27 PM HostServiceBox Running
2/24/2018 8:34:27 PM HostServiceBox Running  <------
2/25/2018 11:22:07 AM HostServiceBox Running <-----
2/25/2018 11:23:07 AM HostServiceBox Running
2/25/2018 11:24:07 AM HostServiceBox Running
2/25/2018 11:25:07 AM HostServiceBox Running
......

在带有 kestrel (.Net Core) 的 IIS 上看起来我的方法睡着了吗?为什么?

is look like on IIS with kestrel (.Net Core) my method slept ? Why?

通常我的 while(true) 重新启动,因为我调用了 API.但是 IHostedService 是一个后台任务,不应该停止吗?

Usualy my while(true) restart because i call the API. But IHostedService is a background task it's shouldnt stop right?

github 上的相关帖子

推荐答案

用户 tym32167 走在正确的轨道上.在 部署:

User tym32167 is on the right track. This is mentioned in the documentation for IHostedService in the section on deployment:

在 IIS 或常规 Azure 应用服务上,您的主机可能会因应用池回收而关闭

on IIS or a regular Azure App Service, your host can be shut down because of app pool recycles

IIS 应用程序池的默认空闲超时时间为 20 分钟,它们的默认应用程序池回收时间也为 29 小时.通常,您希望将空闲超时设置为零(禁用)并将回收设置为一个固定的时间,在那里它会造成最小的伤害.

IIS app pools have a default idle-timeout of 20 minutes, and they also have a default app pool recycle time of 29 hours. Typically you want to set the idle timeout to zero (disabled) and the recycle to a fixed time where it will do the least harm.

有一篇有趣的博文说明他们为什么选择 29 小时 此处,它还涵盖空闲超时.

There's an interesting blog post about why they chose 29 hours here and it also covers idle timeout.

此外,如果您碰巧要部署到 Azure,那么前面链接的那篇文章提出了其他真正全时运行的部署方法(容器、WebJobs 等).

Also, if you happen to be deploying to Azure, that article linked earlier suggests other ways to deploy that will truly run full-time (containers, WebJobs, etc).

这篇关于IHostedService 无故停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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