简体   繁体   中英

Problems with WCF endpoints hosted from Windows Service

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.

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.

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 Why is my .Net app contacting Verisign?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM