Abort() method of wcf client proxy doesn't release session after catching FaultException
Asked Answered
T

2

7

I have created a simple wcf service hosted in IIS and wcf client and figured out that when u catch a FaultException from the wcf service and then call client.Abort() to release the session (as the microsoft samples said) it doesn't release the session and hangs up on the 11th call.

Here is example:

Wcf Service:

[ServiceContract]  
public interface IService1
{
    [OperationContract]
    string GetData(int value);
}


public class Service1 : IService1
{
    public string GetData(int value)
    {
        throw new FaultException("Exception is here");

        return string.Format("You entered: {0}", value);
    }
}

Client:

class Program
{
    static void Main(string[] args)
    {
        Service1Client client = null;          

        for(int i = 0; i < 15; i++)
        {
            try
            {
                client = new Service1Client();
                client.GetData(100);                   
            }
            catch (TimeoutException timeoutEx)
            {
                Console.WriteLine(timeoutEx);
                client.Abort();
            }
            catch (FaultException faultEx)
            {
                Console.WriteLine(faultEx);
                client.Abort();
            }
            catch (CommunicationException commEx)
            {
                Console.WriteLine(commEx);
                client.Abort();
            }
      }  
   }              

}

But if you replace the client.Abort() with client.Close() for catch(FaultException ) then everything works like a charm and there is no locking after 11th call of the wcf-service method.

Why could it be? Why Abort() method doesn't clean up the session after FaultException was catched?

Theola answered 12/10, 2010 at 13:26 Comment(1)
Did you just copy pasted it from here? social.msdn.microsoft.com/Forums/en-US/wcf/thread/…Concertino
F
6

Two things:

  • Abort() should be used when the Communication Channel is in a Faulted state. Using Close() makes the client try to communicate with the service, telling it to close the service instance, graceful-like, if you will. If the the Communication Channel is in the Faulted state, it means no communication can be done from client to service. In that situation you should call Abort() so that at least the client is closed. The service instance/session will still be alive on the server (since there's no communication between the two), and will remain so until the instance timeout occurs. If you had called Close() on a faulted channel, it would have thrown more errors.
  • Your service is throwing a FaultException. This does not mean that the Communication Channel will be put into the faulted state. i.e. you can still make calls using the same client. And as such, in your example you should not be calling Abort().

tl;dr Abort() only closes the client. The service instance/session is still alive.

You can check for the Communication Channel's state by using:

ICommunicationObject comObj = ((ICommunicationObject)client);
if(comObj.State == CommunicationState.Faulted)
   client.Abort();
else
   client.Close();
Fervor answered 1/3, 2012 at 19:24 Comment(0)
D
2

Have you tried this way, which I use for calling WCF?

class Program
{
static void Main(string[] args)
{
    for(int i = 0; i < 15; i++)
    {
        using Service1Client client = new Service1Client()
        {
        try
        {
            client.GetData(100);                   
        }
        catch (TimeoutException timeoutEx)
        {
            Console.WriteLine(timeoutEx);
            client.Abort();
        }
        catch (FaultException faultEx)
        {
            Console.WriteLine(faultEx);
            client.Abort();
        }
        catch (CommunicationException commEx)
        {
            Console.WriteLine(commEx);
            client.Abort();
        }
        finally
        {
            client.Close();
        }
        }
  }  
}              
Dinesh answered 27/9, 2011 at 17:54 Comment(1)
This answer would be more helpful if it included an explanation of WHY you use this method.Parthinia

© 2022 - 2024 — McMap. All rights reserved.