安装后无法启动Windows服务

时间:2018-08-16 02:36:33

标签: c# windows-services

我按照这篇文章来构建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

        }
    }

在Designer视图中,MyService.cs具有一个serviceController1,并且我将serviceName设置为TestService。 ProjectInstaller.cs具有serviceProcessInstaller1serviceInstaller1。 serviceInstaller1中的serviceName属性也为TestService

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

我的设置有问题吗?

2 个答案:

答案 0 :(得分:1)

在构建安装程序时,请确保您处于释放模式,否则#if DEBUG逻辑会启动。

答案 1 :(得分:0)

您需要通知服务控制管理器,您的服务已通过OnStart方法成功启动。 Windows 7/8/10 会忽略此设置,但是 Windows Server 认为您的服务无法正常启动,因此它等待超时并终止您的服务进程。 / p>

通过提供的链接阅读文章的“设置服务状态” 部分。