如何处理 CommunicationObjectFaultedException

How to deal with CommunicationObjectFaultedException

我有一个 returns 返回令牌的 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&amp; 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 个异常都源于通信异常,link 表示根据客户端的状态尝试对它们进行不同的处理。

我们开始吧....

catch (CommunicationException e)
                    {
                        exception = e;
                        if (SSOClient.State != CommunicationState.Faulted)
                        {
                            SSOClient.Close();
                        }
                        else
                        {
                            SSOClient.Abort();
                        }
                        SSOClient = CreateClient();
                    } 

我将异常 cathing 更改为

catch (CommunicationException e)
                    {
                        exception = e;
                        if (SSOClient.State != CommunicationState.Faulted)
                        {
                            SSOClient.Close();
                        }
                        else
                        {
                            SSOClient.Abort();
                        }
                        SSOClient = CreateClient();
                    }

这解决了问题。 MessageSecurityExceptionCommunicationObjectFaultedException 都是从 CommunicationException 扩展而来的,所以这个 catch 抓住了这两个问题。

CommunicationState.Created、关闭、打开、错误。

我不得不专门调用 SSOClient.Open() 让我的代码工作,以防万一这对某人有帮助。