部署时应该 运行 24/7 不循环的控制台服务

console service that should run 24/7 not looping when deployed

我有一个在 s3 中下载和上传 img 的服务,它在本地调试模式下工作并循环 24.7,但每当我在 QA 或 PROD 中部署它时,它在 1 个循环后立即退出。我认为它与内存或s3不允许连续保存有关,但是当我评论除了第一行和最后一行之外的所有代码时,我确认它只运行一次。

这里是调用循环服务的program.cs

static class Program
    {
        private static ILogger Logger = new ServiceLogHelper();
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {


#if (DEBUG)
         
            Library.Code.Utility.EnvironemntUrl = "http://localhost";
#elif (QA)
            Library.Code.Utility.EnvironemntUrl = "http://qa.somesite.com";
  
#elif (PROD)
            Library.Code.Utility.EnvironemntUrl = "http://prod.somesite.com";
 
#endif

            // Set Environment
            if (args.Length > 0)
            {
                var enviromentArgument = args[0];
                if (enviromentArgument.StartsWith("-"))
                {
                    enviromentArgument = enviromentArgument.Substring(1);
                }
                Logger.WriteLine("enviromentArgument " + enviromentArgument + " " + DateTime.Now);
                Library.Code.Utility.EnvironemntUrl = new UriBuilder(enviromentArgument).Uri.ToString();
            }



#if (!DEBUG)
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[]
                {
                    new ImageSomeService()
                };
                ServiceBase.Run(ServicesToRun);
#else
         
            var service = new ImageSomeService();
                System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
            #endif
        }
    }

这里是循环服务

public partial class ImageSomeService : ServiceBase
    {
        public ImageSomeService()
        {
            InitializeComponent();

            OnStart(new string[] { });
        }

        private bool Started { get; set; }

        protected override void OnStart(string[] args)
        {
            Started = true;

            do
            {
                ImageSvcHelper.RunBatch(); 
                System.Threading.Thread.Sleep(10000);
            }
            while (Started);
        }

        protected override void OnStop()
        {
            Started = false; //i put logger here but it was never printed so it means it didn't stop
        }
    }

下面是帮助程序,我只发现了一次运行,因为它只在记录器中打印了一次。但是,如果我在本地(运行 到 Visual studio)处于调试模式,行 "Image service 运行ning in .... "无限打印直到我停止它。

public static class ImageSvcHelper
    {
       
  
        public static bool RunBatch()
        {

           
            Logger.WriteLine("Image caching running in " + Library.Code.Utility.Environment + " " + DateTime.Now);
            var minID = AppHelper.GetValue<int>(group: "LastImgID", key: "ImgID");

            

            Logger.WriteLine("minid " + minID + " " + DateTime.Now);
           //  some codes here
            System.Threading.Thread.Sleep(30000);

            return true;

        }

   
    }

服务控制管理器启动服务,但等待 30 秒 OnStart() 启动服务。因此,您必须 运行 在新线程中循环,让 SCM 退出并管理服务。

protected override void OnStart(string[] args)
{
  Started = true;
  Task.Run(() => {
    do
    {
      ImageSvcHelper.RunBatch(); 
      System.Threading.Thread.Sleep(10000);
    }
    while (Started);
  });
}