Search code examples
wcfwcf-data-serviceswcf-bindingwcf-securitywcf-rest

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


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?


Solution

  • 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?

    Depends on if you need to make subsequent calls to the service. If not then by all means close the connection.

    Is this expected way of calling WCF service proxy? Anyone please suggest if there is some better way handling this.

    No. To handle a problem that is baked into WCF you could should actually be structured like this:

      Documents documnts = null;
    
         try
         {
              var client = new ServiceClientProxy();
              documents = client.GetDocuments();
         }
         finally
         {
             try
             {
                if (client.State != CommunicationState.Closed)
                     client.Close();
              }
              catch
              {
                 client.Abort();
              };
        };
    
        if (documents.Length > 50)
        {
            throw new Exception("Too many Documents");
        }
        else if (documents.Length < 10)
        {
            throw new Exception("Too many Documents");
        }
        else
        {
           return documents;
        }
    

    If you want to truly understand the 'why' I would highly recommend reading this series of articles. They will clear up the Close / Abort portion of your problem.

    http://blogs.msmvps.com/p3net/2014/02/02/a-smarter-wcf-service-client-part-1/
    http://blogs.msmvps.com/p3net/2014/02/09/a-smarter-wcf-service-client-part-2/
    http://blogs.msmvps.com/p3net/2014/02/23/a-smarter-wcf-service-client-part-3/
    http://blogs.msmvps.com/p3net/2014/03/15/a-smarter-wcf-service-client-part-4/
    

    Another other things I should point out with the code you've provided is exceptions should be exceptional.

    Using exceptions for what I would considered to be business logic is usually not the right thing to do. Consider the approach where you return a structured result instead. Perhaps in your case it make sense though.

    HTH