none
WCF Service proxy - Both Close() and Abort functions getting called RRS feed

  • Question

  • I have the below code to call a WCF proxy:-

    var client = new ServiceClientProxy(); try { var documents = client.GetDocuments(); client.Close(); if(documents.Length > 50) { throw new Exception("Too many Documents"); } else if(documents.Length <10) { throw new Exception("Too many Documents"); } else { return documents; } } catch(exception ex) { client.Abort(); }

    Here If the documents count that we get from service is more than 50 or less than 10, in that case we will be calling both Close() as well as the Abort functions on client.Is this expected way of calling WCF service proxy? Anyone please suggest if there is some better way handling this.

    Also is it a better approach to close the client connection immediately after the call or do we need to wait till we have completely used the response properties and close the connection at end?


    • Edited by Vishnu mohanan Friday, May 6, 2016 1:49 PM Edited question
    Friday, May 6, 2016 1:36 PM

Answers

  • Hi Vishnu,

    >>Is this expected way of calling WCF service proxy?

    I would recommend you move the if judgement statement out of try-catch block. This is because if the client does not throw exception, but the documents.Length > 50 or documents.Length <10 throws an exception, it will will execute the method client.Abort(). But when we move the if judgement statement out of the try-catch block, if the client does not throw exception, it will execute the client.Close() method instead of the client.Abort():

    var client = new ServiceClientProxy();
    
    string documents = null;
    
    try
    
      {
    
        documents = client.GetDocuments();
    
        client.Close();
    
       }
    
        catch (CommunicationException e)
       {
          ...
          client.Abort();
       }
       catch (TimeoutException e)
       {
         ...
         client.Abort();
        }
        catch (Exception e)
        {
          ...
          client.Abort();
          throw;
       }
       if(documents.Length > 50)
    
       {
    
         throw new Exception("Too many Documents");
    
       }
    
      else if(documents.Length <10)
    
       {
    
         throw new Exception("Too many Documents");
    
       }
    
      else
    
       {
    
         return documents;
    
       }

    >>Also is it a better approach to close the client connection immediately after the call or do we need to wait till we have completely used the response properties and close the connection at end?

    Yes, I recommend that you close the connection after you use it, and use a try/catch block to report errors and call Abort method. For more information, please refer to:

    https://msdn.microsoft.com/en-us/library/aa355056.aspx .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, May 9, 2016 5:04 AM
    Moderator