Home > Blockchain >  How to deal with CommunicationObjectFaultedException
How to deal with CommunicationObjectFaultedException

Time:11-19

I've got a SOAP request method that returns back a token. For 99% of the time this works fine however 1% of the time I get back a communicationObjectFaultedException.

Is this just unavoidable or is there something in my code that I can improve upon.

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;

                }

The error I get is

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)

It's hard to debug and I can't figure out how to manually throw the exception. When I get the exception I just recreate the client and try again, but this doesn't seem to work. Unless it does try again and errors another few times (attempts > 2).

Am I doing something wrong or is this just something that I have to accept.

Attempt 1

So the 2 exceptions both stem from a communication exception and the link says to try and treat them differently depending on the state of the client.

So here we go....

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

CodePudding user response:

I changed the exception cathing to

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

and this resolved the problem. MessageSecurityException and CommunicationObjectFaultedExceptionboth extend from CommunicationException so makese sense that this catch caught both issues.

  • Related