简体   繁体   English

Windows服务在启动后立即停止

[英]Windows service stops right after launching

I have following problem with the windows service I was writing: When I start the service it stops immediately. 我正在编写Windows服务时遇到以下问题:启动服务时,它会立即停止。 When I was using a console app it wasn't crushing. 当我使用控制台应用程序时,它并没有崩溃。 I have no idea what's the cause of this problem. 我不知道这是什么原因。

Here's the code: 这是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ServiceModel;
using System.ServiceProcess;
using System.Configuration;
using System.Configuration.Install;
using WindowsService;

namespace WS
{

    [ServiceContract(Namespace = "http://WS")]
    public interface INewsReader
    {

    }


    public class NewsReaderService : INewsReader
    {

        public NewsReaderService()
        {
            var config = new Config();

            var scheduled = new Schedule(config);
            scheduled.ExecuteScheduledEvents();
            while (true)
            {
                System.Threading.Thread.Sleep(1000);
                int i = 0;
            }
        }
    }

    public class NewsReaderWindowsService : ServiceBase
    {
        public ServiceHost serviceHost = null;
        public NewsReaderWindowsService()
        {

            ServiceName = "NewsReaderWindowsService";
        }

        public static void Main()
        {
            ServiceBase.Run(new NewsReaderWindowsService());
        }


        protected override void OnStart(string[] args)
        {
            var thread = new System.Threading.Thread(() =>
            {
                while (true)
                {
                    int i = 0;
                    System.Threading.Thread.Sleep(1000);
                }
            });
            thread.Start();

            serviceHost = new ServiceHost(typeof(NewsReaderService));


            serviceHost.Open();

        }

        protected override void OnStop()
        {

        }
    }


    [RunInstaller(true)]
    public class ProjectInstaller : Installer
    {
        private ServiceProcessInstaller process;
        private ServiceInstaller service;

        public ProjectInstaller()
        {
            process = new ServiceProcessInstaller();
            process.Account = ServiceAccount.LocalSystem;
            service = new ServiceInstaller();
            service.ServiceName = "NewsReaderWindowsService";
            Installers.Add(process);
            Installers.Add(service);
        }
    }
}

Well, first of all I think your OnStart method is written badly. 好吧,首先,我认为您的OnStart方法编写得很糟糕。 I can't see the reason for creating a, basicly, empty thread. 我看不出创建一个基本上是空线程的原因。 You should there only initialize service (If necessary), immediately start a new thread that will work for whole time and leave the OnStart method. 您应该只在那里初始化服务(如有必要),立即启动一个将一直工作的新线程,并保留OnStart方法。

Second of all use try catch block, because in my opinion somewhere in there is exception and that's why your windows service stops. 第二个原因是使用try catch块,因为我认为其中的某个地方存在异常,这就是Windows服务停止的原因。

Thirdly see this example WCF Hosting with Windows Service 第三,查看此示例WCF与Windows Service托管

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM