IHostedService没有任何理由停止

btb*_*min 6 c# kestrel .net-core

任何人都可以向我解释为什么我的服务器无缘无故停止了?在我的IHostedService实现之下:

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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是我的日志?

    .....
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
......
Run Code Online (Sandbox Code Playgroud)

在我的方法睡觉的IIS上看起来像是使用kestrel(.Net Core)吗?为什么?

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

github上的相关帖子

McG*_*V10 14

用户tym32167在正确的轨道上.这IHostedService部署部分的文档中提到:

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

IIS应用程序池的默认空闲超时时间为20分钟,并且它们的默认应用程序池回收时间为29小时.通常,您希望将空闲超时设置为零(禁用),并将循环设置为固定时间,以使其受到的伤害最小.

有一篇有趣的博客文章说明了为什么他们在这里选择29小时,它还包括空闲超时.

此外,如果您正在部署到Azure,那么之前链接的那篇文章建议了其他部署方式,它将真正运行全职(容器,WebJobs等).