0

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();
                    } 
Richard Housham
  • 1,525
  • 2
  • 15
  • 31
  • you can refer to [this post](https://stackoverflow.com/questions/2763592/the-communication-object-system-servicemodel-channels-servicechannel-cannot-be) to find a solution. – Lan Huang Nov 09 '21 at 05:49
  • @LanHuang thanks I have read some of those but it's not wholly clear how to best resolve the issue and like I say I get this maybe once a day and I didn't create the SOAP service nor can control it's function. The only thing from that post is the answer and even then it doesn't seem to wholly help me is this. https://stackoverflow.com/a/2763679/4054808. Any other ideas? – Richard Housham Nov 09 '21 at 10:00
  • 1
    Maybe you can try this https://stackoverflow.com/questions/1241331/recovering-from-a-communicationobjectfaultedexception-in-wcf – Lan Huang Nov 10 '21 at 09:05
  • @LanHuang thanks for the link, it did make me think and look at the exception catches and make some changes - see above. It's a bit of a guessing game with regards if it will help or not - but you have to hope ;) Will let you know how I get on. – Richard Housham Nov 10 '21 at 10:03

2 Answers2

0

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.

Richard Housham
  • 1,525
  • 2
  • 15
  • 31
0

You got CommunicationState.Created, closed, opened, faulted.

I had to specifically call SSOClient.Open() to make my code work, just in case this helps someone.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
coder555
  • 23
  • 2