开发者

Windows service connecting to other service over wcf crashes

开发者 https://www.devze.com 2023-02-19 12:55 出处:网络
I have two windows services. One (\'server\') acts as a WCF host to which the other (\'client\') connects. So I have configured a dependency from client to server. Both are also set up to start automa

I have two windows services. One ('server') acts as a WCF host to which the other ('client') connects. So I have configured a dependency from client to server. Both are also set up to start automatically.

When I start these services by hand, everything works fine. When I stop both services and tell client to start, then server will be started before client and all is fine.

However, when I reboot the machine only server is started.

When I add a diagnostic listener I see it got a TimeoutException with the helpful message:

The HT开发者_运维技巧TP request to 'http://[server address]' has exceeded the allotted timeout of 00:00:00. The time allotted to this operation may have been a portion of a longer timeout.

At some other SO question there was an answer that claims WCF is probably confused about what went wrong and therefore starts lying about the timeout.

Did I perhaps miss a dependency for either service? Does WCF require something that hasn't or is being started when client is trying to contact server?


I think you should check your client service. On startup windows services are starting while network devices are still being initialized. Services should be ready to start without network and without any network device. Usual approach is to keep periodic retries to establish connection. You can do little experiment on your machine by uninstalling all network adapters and trying to start up your services.

Additional quick workaround you can do is to setup recovery options on your service -- for example you can configure it to restart service on crash after some timeout -- you can do this through UI in services.msc or in command line using 'sc config' command.


Configuring the dependency between the two Windows Services is not necessarily sufficient to avoid there being a race condition: i.e. to avoid the client service calling the WCF service before the server's WCF channel stack is fully initialised.

The service dependency just ensures that the Windows Service Control Manager won't start the client service process before the server Windows Service has notified the SCM that it has started. Whether this is sufficient depends on how you write the server.

If the server service starts a new thread on which to initialize the WCF stack, your OnStart method is probably returning before the WCF stack is ready for clients. There is then a race condition as to whether the client's first call will succeed.

On the other hand, if the server service does not return from OnStart (and thus doesn't notify the SCM that it has started) until the channel stack is fully open, the dependency removes the race condition, but there is a different pitfall: you need to beware that the SCM's own timeout for starting the Windows service is not triggered while waiting for the WCF stack to initialise, as might well happen on a reboot if the WCF service depends on the network stack, for example. If the server's OnStart does not return within the SCM's timeout, the SCM will not try to start the dependent client service at all, because it does not receive the server's start notification. (There will be a message in the Windows event log from the SCM saying that the server service didn't start within the expected time.) You can extend the SCM timeout by calling ServiceBase.RequestAdditionalTime while the WCF service is being initialised.

Either way, the client service really ought to be written so that it doesn't fail completely if the first WCF call doesn't succeed.

You don't actually say what binding you are using. If client and server services are always running on the same machine, as you seem to indicate, then consider using the NetNamedPipeBinding: then your service won't be dependent on initialization of networking resources and startup should be quicker.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号