【问题标题】:Service is stopping when another program exits当另一个程序退出时服务正在停止
【发布时间】: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


【解决方案1】:

错误在别处...

当代码启动服务时,它无法连接到 Windows 凭据上的数据库...但是如果将登录设置为服务上的特定帐户,它就可以工作...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    相关资源
    最近更新 更多