gpt4 book ai didi

wcf - 在客户端处理 WCF 服务重启

转载 作者:行者123 更新时间:2023-12-04 11:32:09 24 4
gpt4 key购买 nike

我有一个运行在 WCF 上的 GUI 客户端作为 Windows 服务托管在服务器盒上的服务。 WCF 服务在 PerCall InstanceContextMode 中运行,并且客户端有一个服务客户端的单例实例,我想避免在每次调用时重新实例化单例,因为它使我拥有的许多异步调用变得困难。

我的问题是,在 Windows 服务重新启动后,每次客户端进行调用时,它都会收到如下异常消息:

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.



解决这个问题的最佳方法是什么?我可以在对服务客户端的所有调用周围放置 try-catch 子句,并在通信异常时重新实例化单例实例,但这将涉及大量样板代码。

最佳答案

最好的办法是同时避免服务器上的异常。如果 WCF 服务器遇到未被捕获和处理的异常,它将“故障” channel ,使其无用。

在服务器端,可以实现IErrorHandler interface并捕获 .NET 异常,将它们转换为 SOAP 错误,这些错误将更优雅地返回给客户端,而不会导致 channel 出错。

这样,您可以捕获服务器上的所有 .NET 异常,并将它们转换为不会导致这些问题的可互操作的 SOAP 错误。

有关更多信息,请参阅:

  • Specifying and handling faults in contracts
  • WCF Error Handling and Fault Conversion
  • 关于wcf - 在客户端处理 WCF 服务重启,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1985225/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com