我有一個回傳令牌的 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();
}
uj5u.com熱心網友回復:
我將例外處理更改為
catch (CommunicationException e)
{
exception = e;
if (SSOClient.State != CommunicationState.Faulted)
{
SSOClient.Close();
}
else
{
SSOClient.Abort();
}
SSOClient = CreateClient();
}
這解決了問題。
MessageSecurityException并且CommunicationObjectFaultedException兩者都從CommunicationException如此有意義的角度出發,以至于這個捕獲同時解決了這兩個問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/360329.html
