1

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.

Community
  • 1
  • 1
peter
  • 13,009
  • 22
  • 82
  • 142
  • Actually my problem is a bit misguided. Some more info ... this windows service is starting a 'server' so the startup method should be relatively instant (I will time it at some stage). What I mean is that it is not connecting to anything else, so there should be no delay. My logging has actually revealed that the startup method is not called at all when these timeouts occur. I guess too many services are trying to start at once. There is not much I can do about it. Any suggestions? – peter Sep 21 '09 at 21:59
  • Another thought though, I am thinking the startup code in the service is not run, but that is based on log messages not being written to the event log. But what if the start up code is being run, and timing out, and somehow the event log message is not being written to the event log, e.g. it is cached or something. – peter Sep 21 '09 at 22:07

1 Answers1

1

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:

ServiceBase[] ServicesToRun = new ServiceBase[] { new MyWindowsService() };
ServiceBase.Run( ServicesToRun );

In this example, MyWindowsService is 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:

public MyWindowsService()
{
    // service instance initialization goes here...
}

This is where you would initialize the MyWindowsService instance.

Now, the MyWindowsService class should be derived from System.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.

protected override void OnStart(string[] args)
{
    // things to do when starting the service...
}

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:

System.Diagnostics.Debugger.Break();

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.

Matt Davis
  • 45,297
  • 16
  • 93
  • 124
  • Just to confirm Matt. Your response appears to indicate that the service start timeout is calculate from the point at which the entry point is triggered (not the call to ServiceBase.Run). Is that correct? – Richard J Foster May 05 '10 at 18:17