views:

1091

answers:

3

We have two services developed in Visual Studio .NET 2008 with VB.NET. Both were developed completely separately by separate individuals (one of them being myself), but both are reporting the same error during boot: "service hung on starting" appears in the System event log after booting. The services proceed to start up fine after that message (just a few messages down the line is the notice that the service started). Is there something about the slowness of loading the .NET framework or JIT compiling the code that's causing this or what? It starts and stops fine when done manually. Mine is a very simple service with no startup code other than that provided by the framework.

Update 1: This is all I've got in OnStart:

  host = New ServiceHost(GetType(FSE.Licensing.FSELicense))
  host.Open()

FSELicense does not define a constructor, so it just gets the default empty public constructor provided by VB.NET, I guess.

Update 2: My question has morphed based on the solution proposed by a colleague which supposedly fixed the problem. This solution does in fact simply add a dependency on another service that I did not think was necessary since my service does not do anything until a request is made of it. However, it does declare a variable of a COM type. Is it possible that having a reference to this COM type (even though there is no instance) will cause the COM DLL to load at the same time as the service, and possibly rely on a service? I didn't realize COM references were like static links in .NET if so.

A: 

This is a guess, but during system startup, so many things are loading behind the scenes that this slows things down already.

Then you also have to worry about components that rely on other services to be started. For example, if your service relied on the SQL Server Agent, which relies on the SQL Server Database Engine services, you'd have to wait for the to load in the order of the dependencies.

So...

Depending on what FSE.Licensing.FSELicense does internally, it may be waiting for other services that it depends on to load first, or it could just be that the machine is slow, and loading all of the background processes is happening at the same time and your service is simply competing for resources with all of those processes.

However, the warning you're seeing i just a warning. I've seen this on some of my services that can take a while to load. This is obvious and I probably don't even need to say it, but the service control manager expects services to load within a specific time period (I'm not sure what that is) but if a service appears to hang, it logs this message but in the meantime, the service is still trying to load. As long as your service is starting, I wouldn't worry too much about this warning, unless you can see some way to code it better to avoid this.

One thing you could check is in the Services console, look at the properties of your service and look at the "Dependencies" tab. it may be blank, but it may give you some insight.

David Stratton
This problem happens on most systems, including new fast systems without any bloat. And only the .NET-based services are reporting these errors. We have accepted that it is just a warning until now, but multiple customers are calling with questions about these, and now we're being pushed for answers. The service has no dependencies, and like I said, it's a very simply service. Are other people creating VB.NET services out there and not having this problem?
BlueMonkMN
Sorry.. I only have this problem with one of my services, and I know why that takes so long to start when it happens. I've never run into this situation with a very simple service, and I've written quite a few. Sorry I couldn't be of more help.
David Stratton
+1  A: 

I solved my problem by adding a service dependency to the service for "HTTP SSL service" (HTTPFilter). I had the exact same problem on an XP machine with a Windows Service that I wrote in .NET that exposed a WCF endpoint with a netpipe and http binding. When the system started up, it would always hang on the host.Open() call, and eventually timeout. I could start the service manually with no problem.

See this link for instructions on adding a service dependency. The DependOnService value that I used was HTTPFilter

Scott Fletcher
I have fixed this issue by adding service dependency on IPSEC service (PolicyAgent)
Vadmyst
Randomly adding a dependency on another service is a fragile solution.
oefe
I would agree with oefe, though our solutions were not random; they were scientifically proven to have an effect. (I myself spent two hours testing the HTTPFilter dependency to make sure that the results were repeatable.) The problem with our solutions is that we do not understand "why" they alleviated the problem.
Scott Fletcher
A: 

Try to minimize the processing in OnStart; in particular, avoid blocking for an unknown amount of time; e.g. don't try to connect remote computers -- no database queries, no service requests.

If you need to do something that might block or otherwise take a long time, spawn a separate thread for this task if possible.

However, you cannot always avoid blocking completely, as you need to make sure that your services is operational when you return from OnStart. In such cases, you can use ServiceBase.RequestAdditionalTime to request more time for OnStart to finish.

oefe