安装后无法启动 Windows 服务

Could not Start up Windows Service after Installation

我按照这篇文章构建了我的 windows 服务应用程序。

https://docs.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer

现在我的项目目录有MyService.csProjectInstaller.cs。两者都包含一个 Designer.cs 和一个 .resx 文件。

构建项目后,我将 /bin/Debug 下的所有内容复制到服务器,然后 运行 InstallUtil 命令进行安装。

我可以在“服务”列表中看到我的服务,但是当我单击“开始”时,需要很长时间才能启动,直到超时。

我的Program.cs文件很简单

public static void Main()
        {


#if DEBUG

            MyService mySvc = new MyService();
            mySvc.OnDebug();
            System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
#else

            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new MyService() 
            };
            ServiceBase.Run(ServicesToRun);
#endif
        }

MyService.cs中:

public partial class MyService : ServiceBase
    {
        private bool stopping = false;
        private Int32 timeInterval = 0;
        private ManualResetEvent stoppedEvent;
        public static IServiceProvider svcProvider = null;

        public MyService()
        {
            InitializeComponent();
            stopping = false;
            stoppedEvent = new ManualResetEvent(false);
            LoadDIStartup();
        }

        public void OnDebug()
        {
            StartServiceWorkerMainProcess();
        }

        protected override void OnStart(string[] args)
        {
            stopping = false;
            StartServiceWorkerMainProcess();
        }

        protected override void OnStop()
        {
            stopping = true;
            StopServiceWorkerMainProcess();
            stoppedEvent.WaitOne(); //Wait for the finish of the ServiceWorkerThread
        }

        /// <summary>
        /// The function called in the start event of the the service. And when in Visual Studio Debug Mode run.
        /// </summary>
        public void StartServiceWorkerMainProcess()
        {
            try
            {
                timeInterval = AppConfig.TimeInterval;
                // Queue the SubWorker for execution in a worker thread. 
                ThreadPool.QueueUserWorkItem(new WaitCallback(ServiceWorkerSub));
            }
            catch (Exception e)
            {
                AppLogger.LogError(" Error while launching the WorkerSub thread. " + "\n" + e.Message + "\n" + e.InnerException + " \n" + e.StackTrace + "\n" + e.Source + "\n");

            }
        }

        /// <summary>
        /// The function called in the stop event of the the service
        /// </summary>
        public void StopServiceWorkerMainProcess()
        {
            AppLogger.LogInfo(" Service Stopped at " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + "\n");
        }

        private async void ServiceWorkerSub(object state)
        {
            try
            {
                // Periodically check if the service is stopping
                while (!stopping)
                {
                    try
                    {
                        //Do my Stuff (FYI, Mutex is used here.)
                        AppLogger.LogInfo("DONE. About to sleep.");
                    }
                    catch (Exception e)
                    {
                        AppLogger.LogError(" Error. " + "\n" + e.Message + "\n" + e.InnerException + " \n" + e.StackTrace + "\n" + e.Source + "\n");

                    }
                    Thread.Sleep(timeInterval);
                }
                // Signal the stopped event
                stoppedEvent.Set();
            }
            catch (Exception e)
            {
                AppLogger.LogError(" Error in ServiceWorkerSub. " + "\n" + e.Message + "\n" + e.InnerException + " \n" + e.StackTrace + "\n" + e.Source + "\n");

            }
        }

        private static void LoadDIStartup()
        {
            //Dependency Injection Setup Start
            // blah blah. DI using json files. 
            //Dependency Injection Setup End

        }
    }

在设计器视图中,MyService.cs 有一个 serviceController1,我设置了 serviceName = TestService。 ProjectInstaller.cs 有一个 serviceProcessInstaller1serviceInstaller1。 serviceInstaller1 中的 serviceName 属性 也是 TestService

该项目内部有多个属性文件,在本地运行良好。

我的设置有问题吗?

当您构建安装程序时,请确保您处于发布模式,否则您的 #if DEBUG 逻辑将启动。

您需要通知 服务控制管理器 ,您的服务已在 OnStart 方法中成功启动。 Windows 7/8/10 忽略了这一点,但是 Windows Server 认为您的服务没有正常启动, 所以它会等待超时并终止您的服务进程。

阅读您提供的 link 文章的 "Setting Service Status" 部分内容。