Dealing with WCF service restart on client-side

大城市里の小女人 提交于 2019-12-03 07:50:22

问题


I've got a GUI client which is running against a WCF services hosted as a Windows service on a server box. The WCF service is running in PerCall InstanceContextMode, and the client has a singleton instance of the service client and I want to avoid reinstantiating the singleton on every call as it makes life difficult for the many asynchronous calls I have.

The problem for me is, after the Windows service is restarted, everytime the client makes a call it gets an exception message like this:

This channel can no longer be used to send messages as the output session was auto-closed due to a server-initiated shutdown. Either disable auto-close by setting the DispatchRuntime.AutomaticInputSessionShutdown to false, or consider modifying the shutdown protocol with the remote server.

What's the best way to get around this? I can put try-catch clauses around all the calls to the service client and reinstantiate the singleton instance on communication exceptions but that will involve a lot of boilerplate code..


回答1:


The best would be to avoid the exceptions on the server all together. If a WCF Server encounters an exception that is not being caught and handled, it will "fault" the channel, rendering it useless.

On the server side, you can implement the IErrorHandler interface and catch .NET exceptions, turning them into SOAP faults which will be handed back to the client more gracefully, without faulting the channel.

That way, you can catch all .NET exceptions on the server, and convert them into interoperable SOAP faults which will not cause these problems.

For more information, see:

  • Specifying and handling faults in contracts
  • WCF Error Handling and Fault Conversion



回答2:


You can take a look at this to perhaps avoid some of that boilerplate code:

http://wcfproxygenerator.codeplex.com



来源:https://stackoverflow.com/questions/1985225/dealing-with-wcf-service-restart-on-client-side

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!