如何处理 CommunicationObjectFaultedException

How to deal with CommunicationObjectFaultedException

提问人:Richard Housham 提问时间:11/8/2021 最后编辑:Richard Housham 更新时间:3/12/2022 访问量:1130

问:

我有一个返回令牌的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

因此,这两个异常都源于通信异常,并且链接说要尝试根据客户端的状态以不同的方式对待它们。

所以我们开始......

catch (CommunicationException e)
                    {
                        exception = e;
                        if (SSOClient.State != CommunicationState.Faulted)
                        {
                            SSOClient.Close();
                        }
                        else
                        {
                            SSOClient.Abort();
                        }
                        SSOClient = CreateClient();
                    } 
C# WCF 异常 SOAP

评论

0赞 Lan Huang 11/9/2021
您可以参考这篇文章来找到解决方案。
0赞 Richard Housham 11/9/2021
@LanHuang,谢谢,我已经阅读了其中的一些内容,但并不完全清楚如何最好地解决问题,就像我说的那样,我可能每天收到一次,我没有创建SOAP服务,也无法控制它的功能。那篇帖子中唯一的东西就是答案,即便如此,它似乎也不能完全帮助我。stackoverflow.com/a/2763679/4054808。还有其他想法吗?
1赞 Lan Huang 11/10/2021
也许你可以试试这个 stackoverflow.com/questions/1241331/......
0赞 Richard Housham 11/10/2021
@LanHuang感谢您的链接,它确实让我思考并查看异常捕获并进行一些更改 - 见上文。关于它是否会有所帮助,这有点像猜谜游戏——但你必须希望;)会让你知道我是怎么过的。

答:

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();
                    }

这解决了问题。 两者都从如此有意义的地方延伸出来,以至于这个捕获抓住了这两个问题。MessageSecurityExceptionCommunicationObjectFaultedExceptionCommunicationException

0赞 coder555 3/12/2022 #2

你得到了,关闭了,打开了,出了故障。CommunicationState.Created

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