IHostedService停止,没有任何原因 [英] IHostedService Stop without any reason
问题描述
有人可以向我解释为什么我的服务器无缘无故停止吗? 在我的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
......
在使用红k(.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?
推荐答案
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,则前面链接的文章建议了其他可以真正全天候运行的部署方式(容器,WebJob等).
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屋!