joaocarlospf joaocarlospf - 1 year ago 130
C# Question

Windows Service stuck on "starting" status as local system account

I developed a http server via console application in C# and decided to turn it into a Windows service to be able to initialize it without the need to login the machine.

I followed all the steps in How to create Windows Service and chose the account as "Local System", but when I install in my server machine and push the start button it takes a while and gives the following error:

Erro 1053: The service did not respond to the start or control request in timely fashion.

After that, the service status stays stuck in "starting" and the application don't work and I can't even stop the service anymore.

Trying to work around this problem, I changed it to "Network Service", so it started normally, but the application was not listening in the port I set when I checked in the prompt with the command "netstat -an". But the application listens normally if i run it as a console application.

So I am looking for an answer to one of these two questions:

  1. What should I do to make the service starts properly with a Local System account?

  2. If I decide to use Network service account, what should I care about to guarantee that my service works properly as a server?

Answer Source

When I converted my console application to windows service I simply put my code directly in the OnStart method. However, I realized the OnStart method should start the service, but needs to end some time to the service indeed start. So I created a thread that runs my service and let the OnStart method finish. I tested and the service worked just fine. Here is how it was the code:

protected override void OnStart(string[] args)
    Listener(); // this method never returns

Here is how it worked:

protected override void OnStart(string[] args)
    Thread t = new Thread(new ThreadStart(Listener));

But I still don't understand why the service ran (passed the "starting" status, but didn't work) when I used network service account. If anyone knows, I'll be glad to know the reason.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download