【发布时间】:2017-06-23 13:39:59
【问题描述】:
我有一个服务,做了一个小程序来监控服务,如果它宕机了就重启...
我的问题是,如果我关闭监控程序,服务就会停止,我不知道为什么......
我的程序如下所示
public static void Main(string[] args)
{
Task.Run(async () =>
{
var sc = new ServiceController("Explore");
var client = new HttpClient();
while (DateTime.Now.Hour >= 8 &&
DateTime.Now <= new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59))
{
sc.Refresh();
await Task.Delay(2000);
if (sc.Status != ServiceControllerStatus.Stopped && sc.Status != ServiceControllerStatus.Paused)
{
await Task.Delay(300000);
continue;
}
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 10));
sc.Refresh();
// starts services that handle notifications
await client.GetAsync($"{host}/sqltorabbit/start");
await client.GetAsync($"{host}/notifications/indexer/Elastic/start");
await client.GetAsync($"{host}/notifications/push/Apple/start");
}
await Task.Delay(28800000);
}).Wait();
}
}
【问题讨论】:
-
你在哪里托管它?天蓝色? IIS?它们都会在某些条件下(内存压力、空闲时间)等回收您的应用程序,这是 IIS 的正常行为,因此不要依赖您的应用程序始终处于启动状态。如果您需要一直运行的东西,请将其作为后台进程运行,即在 IIS 之外托管/运行的单独 exe
-
该服务托管在我自己的机器上,并且是一个启动owin WebApp的控制台,所以我也不应该担心iis回收
标签: asp.net-core windows-services