简体   繁体   中英

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. 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. 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.

Second of all use try catch block, because in my opinion somewhere in there is exception and that's why your windows service stops.

Thirdly see this example WCF Hosting with Windows Service

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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