views:

145

answers:

3

I have a managed Windows Service that hosts a couple of WCF endpoints. The service is set to start automatically when the PC is restarted. On reboot I find that this line of code:

ServiceHost wcfHost1 = new ServiceHost(typeof(WCFHost1));

in the OnStart() method of the service takes somewhere between 15 - 20 seconds to execute. Actually I have two such statements but the second one executes in a flash. It is the first one that takes so long. Does anyone know what could be causing the bottleneck? Because of this, sometimes the call exceeds 30 seconds and as a result the SCM thinks my service timed out while trying to initialize itself. Now, I know its easy for me to just spin off a thread to do this and return from OnStart() right away but I'd like to know what could cause this delay.

This happens only when the service starts up on PC reboot. If the PC is up and running, the service starts & stops in less than a second.

A: 

This might provide more help. Basically, I think you need to figure out what dependencies you have and add them to your service, so they start before yours.

This is just a shot in the dark, but could it be that the .net framework hasn't loaded yet. Perhaps, you can try to set the Automatic Start to a Delayed Automatic Start, which will allow the .net framework and other windows services time to start.

Also, when a .net application starts up, it is compiled with the Just-In-Time compiler. This could be waiting for .net to compile it.

Lastly, depending on the type of WCF Instance you are using, you could have an issue with the constructor taking awhile to initialize.

Hope this helps.

daub815
Dilip
@daub815: Sorry I didn't see your link the first time. Mine is not all that complicated. That simple ServiceHost instantiation line in my OP is where all the bottleneck lies.
Dilip
That simple line does a lot more under the covers and if it is only occurring on start up, then there is another service that is needs to start up before yours.
daub815
A: 

I have had problems with a Windows Service that took a long time to start. I found out that it was because of signed assemblies I used (Enterprise Library). .NET was trying to verify the Authenticode signature when the assembly is loaded (maybe your constuctor is using a type for the first time which causes loading that assembly). Just try adding the following to your app.config. When it doesn't work you have another problem.

<configuration>
    <runtime>
        <generatePublisherEvidence enabled="false"/>
    </runtime>
</configuration>

You can also find some extra information here: http://support.microsoft.com/kb/936707 and http://stackoverflow.com/questions/1923325/why-is-my-net-app-contacting-verisign

BasvdL
A: 

There is no problem with your WCF service. The best way to resolve your issue is to create a dummy operation that the client must call before anything else. This will force your client to do its handshake on your service prior to your real transaction. I actually used dummy operation to call my service every now and then to see if it is still alive.

powerbox
@powerbox: sorry, but his problem happens on `ServiceHost` open, not on an operation call.
John Saunders