C# WCF 无法用于通信,因为它处于 Faulted 状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9409329/
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
WCF Cannot be used for communication because it is in the Faulted state
提问by espvar
When i try use a webservice i get the following exception. My main question is when does this exception happen? on the server or client? where is the error? Does the server throw this for a wide range of faults?
当我尝试使用网络服务时,出现以下异常。我的主要问题是这个异常什么时候发生?在服务器或客户端?错误在哪里?服务器是否会因各种故障而抛出此问题?
I did some changes on my own that seems to work
我自己做了一些似乎有效的改变
It actually works now. I removed using and added som cleanup on the service client.
它现在确实有效。我删除了 using 并在服务客户端上添加了 som 清理。
if (Service != null && Service.State != CommunicationState.Faulted)
{
success = true;
Service.Close();
}
}
catch (Exception ex)
{
msg = "Error" + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace;
}
finally{
if (!success)
{
if (Service != null) Service.Abort();
}
}
This was the exception:
这是个例外:
The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.
Server stack trace:
at System.ServiceModel.Channels.CommunicationObject.Close(TimeSpan timeout)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at System.ServiceModel.ICommunicationObject.Close(TimeSpan timeout)
at System.ServiceModel.ClientBase`1.System.ServiceModel.ICommunicationObject.Close(TimeSpan timeout)
at System.ServiceModel.ClientBase`1.Close()
at System.ServiceModel.ClientBase`1.System.IDisposable.Dispose()
at bNet.Services.Customers.Cres.Helios.ServiceForm.Send(ServiceFormAction task) in C:\bNetProjects\bNet Web Tools\Solution root\bNet.Services\Customers\Cres\Helios\ServiceForm.cs:line 99
at bNet.Web.Sites.Public.Customers.Cres.ServiceSkjema.Units.Page.ServiceFormControl.SubmitFormClick(Object sender, EventArgs e) in C:\bNetProjects\bNet Web Tools\Solution root\bNet.Web.Sites.Public\Customers\Cres\ServiceSkjema\Units\Page\ServiceFormControl.ascx.cs:line 192
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
采纳答案by Henk Holterman
Faulted state means there has been an unexpectedexception on the server side. In an earlier call.
故障状态意味着服务器端出现了意外异常。在之前的电话中。
You should have gotten an exception at the client side too, maybe your code ignores it?
您也应该在客户端遇到异常,也许您的代码忽略了它?
You can solve it by reopening the connection. But it seems you need better error handling.
您可以通过重新打开连接来解决它。但似乎您需要更好的错误处理。
回答by Ryan Rodemoyer
This error can also be caused by having zero methods tagged with the OperationContract attribute. This was my problem when building a new service and testing it a long the way.
此错误也可能是由使用 OperationContract 属性标记的零个方法引起的。这是我在构建新服务并对其进行长期测试时遇到的问题。
回答by Zac
Insteadof using the usingstatement, try running your code without it.
不要使用该using语句,而是尝试在没有它的情况下运行您的代码。
From
从
using(var client = new WCFClient())
{
// ... code
}
to
到
var client = new WCFClient()
// ... code
Upon doing so, we were able to see that the original WCF Cannot be used for communication because it is in the Faulted statemessage was caused by the using()call itself. Why?Our code that was using the WCF client was passing in invalid credentials, and the server responded with an error and changing the state of the proxy to faulted. The using()block, as we know, calls Dispose()on the object - in this case our WCF client.
这样做后,我们能够看到原始WCF 无法用于通信,因为它处于故障状态消息是由using()调用本身引起的。为什么?我们使用 WCF 客户端的代码传递了无效凭据,服务器响应错误并将代理状态更改为故障。该using()块,因为我们知道,调用Dispose()的对象-在这种情况下,我们的WCF客户端。
Because the WCF client failed, and the WCF client was in a faulted state, calling Dispose()caused the error WCF Cannot be used for communication because it is in the Faulted stateto be thrown.
因为WCF客户端失败,而WCF客户端处于故障状态,调用Dispose()导致错误WCF无法用于通信,因为它处于故障状态被抛出。
We were able to see this by wrapping the code that uses the WCF client in a try...catchblock.
通过将使用 WCF 客户端的代码包装在一个try...catch块中,我们能够看到这一点。

