I found this post,
windows service startup timeout
But just wanted to clarify, I have written a windows service in C# .NET that gives timeout errors at times, e.g.
A timeout was reached (30000 milliseconds) …. when starting the service
The reason is it does some WCF stuff in the start up method which works fine most of the time, but if your computer is bogged down on startup for instance it can cause it to take a bit longer. I realise now that the code shouldn’t be structured like that.
Is an acceptable solution to start a background thread from the startup method and finish doing the WCF stuff in there?
Thanks.
Like all applications, a Windows service has an entry point. In C#, this is called Main() and is a static method on some class. Inside the Main() function of your Windows service, you should have something akin to this:
In this example,
MyWindowsServiceis the name of the Windows service class to run and should be replaced with whatever your Windows service class name is.When this code is executed in Main(), the default constructor for your Windows service class will be called, which looks something like this:
This is where you would initialize the
MyWindowsServiceinstance.Now, the
MyWindowsServiceclass should be derived fromSystem.ServiceProcess.ServiceBase. If that is true, then you can override the OnStart() method, which is called when a Start command is sent to the service by the Service Control Manager.Once the OnStart() function returns, your service is effectively running, i.e., started.
So, the question is where along this chain of events your delay is occurring – in Main(), the service constructor, or the OnStart() callback method. Have you tried debugging your service? An easy way to do this is to place the following line of code inside the Main() function of your service:
When you start the service, you will be prompted to select a debugger. Simply select a new instance of Visual Studio, and you’ll jump right into the code at the point where this Break() call is made. You can debug from there, setting breakpoints in the pertinent places (constructor, OnStart()) to see where the hangup is occurring.