Search code examples
c#windowsserviceonstart

C# Windows Service does not appear as started


I wrote a little windows service in C#, which should listen on a specific port and do something with a request.

I also wrote the Main like this:

using System;
using System.ServiceProcess;

namespace AutoDeployService
{
    public static class WindowsServiceController
    {
        private static void Main(string[] args)
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new AutoDeployService() 
            };

            if (Environment.UserInteractive)
            {
                new AutoDeployService().ConsoleRun();
            }
            else
            {
                System.Diagnostics.Debugger.Break();
                ServiceBase.Run(ServicesToRun);
            }
        }
    }
}

So I can start the service with console and without. When i start the program as Console-Application, it works fine!

If I install the service with "InstallUtil.exe" it installs perfect and it appears in the list of services. But when I try to start the service it gives me after about a half minute or more this message:

The service did not respond to the start or control request in a timely fashion.

After I researched this error in www I found, that the OnStart-Method can take much time, now my OnStart-Method looks like:

        protected override void OnStart(string[] args)
        {
            base.OnStart(args);
            var initializeThread = new Thread(new ThreadStart(Initialize));
            initializeThread.Start();
        }

So I hope anyone can help me.

Greetings

EDIT: It seems that the service started after I start him with servicemanager, but in servicemanager the error above appears and its defined as not started.

Eventlog:

Name der fehlerhaften Anwendung: AutoDeployService.exe, Version: 1.0.0.0, Zeitstempel: 0x5459e290 Name des fehlerhaften Moduls: unknown, Version: 0.0.0.0, Zeitstempel: 0x00000000 Ausnahmecode: 0x00000000 Fehleroffset: 0x0039010b ID des fehlerhaften Prozesses: 0xa0c Startzeit der fehlerhaften Anwendung: 0x01cff8d816b830d8 Pfad der fehlerhaften Anwendung: C:\Users*username*\Desktop\AutoDeploy\AutoDeployService\bin\Debug\AutoDeployService.exe Pfad des fehlerhaften Moduls: unknown Berichtskennung: 5d636ccf-64cb-11e4-b5d1-0050568bc9b7

EDIT:

If I start the service, the service doesnt seems to get into the OnStart-Method, because he doesnt creates a folder.


Solution

  • Thank you for your help.

    I found my erorr with attaching the Debugger with this command:

    Debugger.Launch().
    

    Error was in a Thread from my application.