首页 > 解决方案 > 安装后无法启动 Windows 服务

问题描述

我按照这篇文章构建了我的 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如此。

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

我的设置有问题吗?

标签: c#windows-services

解决方案


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


推荐阅读