提问人:Richard Housham 提问时间:11/8/2021 最后编辑:Richard Housham 更新时间:3/12/2022 访问量:1130
如何处理 CommunicationObjectFaultedException
How to deal with CommunicationObjectFaultedException
问:
我有一个返回令牌的SOAP请求方法。在 99% 的情况下,这工作正常,但是有 1% 的时间我返回 communicationObjectFaultedException。
这是不可避免的,还是我的代码中有一些我可以改进的地方。
MyToken Token = new MyToken ();
Exception exception = null;
bool TokenSet = false;
int attempts = 0;
while(TokenSet == false && attempts <= 2)
{
try
{
MyToken = SSOClient.GenerateSsoToken(id.ToString(), null, null, winframe, null);
TokenSet = true;
exception = null;
}
catch (MessageSecurityException e)
{
exception = e;
SSOClient.Close();
SSOClient = CreateClient();
}
catch(CommunicationObjectFaultedException e)
{
exception = e;
//SSOClient.Close(); can't close what is faulted - I think this is causing some issue once a day or so...
SSOClient = CreateClient();
}
attempts = attempts + 1;
}
我得到的错误是
System.ServiceModel.CommunicationObjectFaultedException: 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)
很难调试,我无法弄清楚如何手动抛出异常。当我遇到异常时,我只是重新创建客户端并重试,但这似乎不起作用。除非它重试并再次出错几次()。attempts > 2
是我做错了什么,还是这只是我必须接受的事情。
尝试 1
因此,这两个异常都源于通信异常,并且链接说要尝试根据客户端的状态以不同的方式对待它们。
所以我们开始......
catch (CommunicationException e)
{
exception = e;
if (SSOClient.State != CommunicationState.Faulted)
{
SSOClient.Close();
}
else
{
SSOClient.Abort();
}
SSOClient = CreateClient();
}
答:
0赞
Richard Housham
11/19/2021
#1
我将异常 cathing 更改为
catch (CommunicationException e)
{
exception = e;
if (SSOClient.State != CommunicationState.Faulted)
{
SSOClient.Close();
}
else
{
SSOClient.Abort();
}
SSOClient = CreateClient();
}
这解决了问题。 两者都从如此有意义的地方延伸出来,以至于这个捕获抓住了这两个问题。MessageSecurityException
CommunicationObjectFaultedException
CommunicationException
0赞
coder555
3/12/2022
#2
你得到了,关闭了,打开了,出了故障。CommunicationState.Created
我不得不专门打电话让我的代码工作,以防万一这对某人有帮助。SSOClient.Open()
评论