【发布时间】:2021-12-21 10:52:01
【问题描述】:
我有一个返回令牌的 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
因此,这 2 个异常都源于通信异常,并且链接说根据客户端的状态尝试对它们进行不同的处理。
所以我们开始......
catch (CommunicationException e)
{
exception = e;
if (SSOClient.State != CommunicationState.Faulted)
{
SSOClient.Close();
}
else
{
SSOClient.Abort();
}
SSOClient = CreateClient();
}
【问题讨论】:
-
您可以参考this post寻找解决方案。
-
@LanHuang 谢谢我已经阅读了其中的一些内容,但目前还不清楚如何最好地解决这个问题,就像我说我可能每天收到一次,我没有创建 SOAP 服务,也不能控制它的功能。那篇文章中唯一的答案就是答案,即使这样,它似乎也不能完全帮助我。 stackoverflow.com/a/2763679/4054808。还有其他想法吗?
-
@LanHuang 感谢您的链接,它确实让我思考并查看异常捕获并进行一些更改 - 见上文。关于它是否会有所帮助,这有点像猜谜游戏 - 但你必须希望;)会让你知道我的进展情况。