C# WCF 错误通信对象 System.ServiceModel.Channels.ServiceChanne 无法用于通信,因为它处于故障状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/743675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:27:11  来源:igfitidea点击:

WCF Error The communication object, System.ServiceModel.Channels.ServiceChanne, cannot be used for communication because it is in the Faulted state

c#wcf

提问by

We get

我们得到

"The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state."

“通信对象 System.ServiceModel.Channels.ServiceChannel 无法用于通信,因为它处于故障状态。”

message when we close the application. Can anyone tell me how to fix it? We know it is communication channel trying to close but it is not able to close due to service not available or in faulted state.

当我们关闭应用程序时的消息。谁能告诉我如何修复它?我们知道它是通信通道试图关闭,但由于服务不可用或处于故障状态而无法关闭。

All I can say is, when the service is not available, but the Garbage collector trying to destroy the object, the communication objects is calling its service Close function. There we get exception.

我只能说,当服务不可用,但垃圾收集器试图销毁对象时,通信对象正在调用其服务关闭函数。在那里我们得到了例外。

采纳答案by John Saunders

When you ask a question about an exception, you should post the entire exception, including all InnerException instances. You should catch the exception, display ex.ToString(), then rethrow the exception with "throw":

当您询问有关异常的问题时,您应该发布整个异常,包括所有 InnerException 实例。您应该捕获异常,显示 ex.ToString(),然后使用“throw”重新抛出异常:

try {
    // Do whatever causes the exception
} catch (Exception ex) {
    Console.WriteLine(ex.ToString());  // Or Debug.Print, or whatever
    throw; // So exception propagation will continue
}

In this case, I wonder if you have a usingblock around your proxy instantiation:

在这种情况下,我想知道您的代理实例化周围是否有using块:

using (var proxy = new WcfProxyClient())
{
    // Use of proxy
}

There is a design flaw in WCF that makes this about the only place in .NET where you should not use a usingblock. Instead, you need to do it by hand. See http://web.archive.org/web/20100703123454/http://old.iserviceoriented.com/blog/post/Indisposable+-+WCF+Gotcha+1.aspx.

WCF 中有一个设计缺陷,这使得它成为 .NET 中唯一不应该使用using块的地方。相反,您需要手动完成。参见http://web.archive.org/web/20100703123454/http://old.iserviceoriented.com/blog/post/Indisposable+-+WCF+Gotcha+1.aspx

Also, see "What is the best workaround for the WCF client usingblock issue?" and "Indisposable WCF clients".

另外,请参阅“ WCF 客户端using块问题的最佳解决方法是什么?”和“ Indisposable WCF 客户端”。

回答by Codebrain

Take a look at the proxies project here.

看看这里的代理项目。

We had a similar problem and this technique fixed it. It basically involves inheriting from a class which will automatically recreate the channel if it faults.

我们有一个类似的问题,这个技术修复了它。它基本上涉及从一个类继承,如果它出现故障,它将自动重新创建通道。