DimDom
DimDom

Reputation: 27

C# Running console application as windows service - The service didn't respond error

I'm trying to run some console app as windows service, I followed this question, and I made a few changes to make it fit to my app.

My main code looks like that:

public static class Program
{
    public class Service : ServiceBase
    {

        public Service(string serviceName)
        {
            this.ServiceName = serviceName;
        }

        protected override void OnStart(string[] args)
        {
            Program.Start(args);
        }

        protected override void OnStop()
        {
            Program.Stop(this.ServiceName);
        }
    }
    #endregion

    static void Main(string[] args)
    {
        if (!Environment.UserInteractive)
            // running as service
            using (var service = new Service("TestService"))
                ServiceBase.Run(service);
        else
        {
            // running as console app
            Start(args);
        }
    }

    private static void Start(string[] args)
    {
        while(true)
        {
            //DO SOMTHING
        }
    }

    private static void Stop(string serviceName)
    {
        //Writing to log that 'serviceName' stopped
    }
}

I tried to run the following console app as a service, by using the following steps:

1) Use the command: sc create ServiceTestName123 binPath= "PATH TO THE EXE FILE IN THE PROJECT DEBUG FOLDER".

2) Use the command: sc start ServiceTestName123 "parameter1".

And I got an error: "StartService FAILED 1053: The service did not respond to the start or control request in a timely fashion"

I read about the error in the internet and found out that I can try to solve this problem by running the start function with another thread, so I updated the OnStart function to the following function:

protected override void OnStart(string[] args)
{
    Thread t = new Thread(() => Program.Start(args));
    t.Start();
}

After trying to re-create the service (delete the old one and create the service again with the new OnStart function) and re-run it I got the same error.

By the way, when I ran this code as console app it worked properly.

Could someone please explaing me what am I doing wrong?

Thanks a lot.

Upvotes: 2

Views: 5546

Answers (2)

leo begher
leo begher

Reputation: 615

To debug what's going on, you can attach the debugger at the very beggining of your program start up.

This way, you can check what your program is doing.

You can also check in the Windows ever viewer, the error that windows is throwing.

Put this line at the start trace of your program:

System.Diagnostics.Debugger.Launch()

Upvotes: 0

Vikhram
Vikhram

Reputation: 4404

I tried your exact steps and it worked for me. I will highlight a few key points that I came across

  1. OnStart should definitely return in a timely fashion. i.e. the work should happen in a separate process/thread. I used your code for thread and it worked fine for me.
  2. Make sure the executable is on a local drive which can be accessed from your "Local System" account without any permission issues.
  3. Make sure when you create the service, you provide the absolute path and not relative path.
  4. Make sure the sc create ... command responds back with [SC] CreateService SUCCESS
  5. Check the service was created in the service control panel
  6. Make sure you can start it from the service control panel before attempting it from command line
  7. Also, open task manager or process explorer to make sure the service executable is running or not (irrespective of what status is returned by service control panel or scm start)
  8. For debugging, I logged the information into a local temp file - again watch out for permissions issues with Local System account.
  9. If for whatever reasons you have to delete the service, make sure that it indeed disappeared from the service control panel

Upvotes: 1

Related Questions