Abort () wcf proxy method does not free session after detecting FaultException

I created a simple wcf service hosted in IIS and the wcf client and found out that when you catch a FaultException from the wcf service and then call client.Abort () to free the session (as Microsoft samples said), 'release the session and hang up the handset on the 11th call.

Here is an 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 client.Abort () with client.Close () for catch (FaultException), then everything works like a charm, and after the 11th call to the wcf service method, there is no lock.

Why? Why doesn't the Abort () method clear the session after a FaultException is detected?

+6
c # wcf faultexception
source share
2 answers

Two things:

  • Abort() should be used when the link is in the Faulted state. Using Close() , the client tries to contact the service, telling it to close the service instance, elegant, if you like. If the communication channel is in the Faulted state, this means that communication between the client and the service is impossible. In this situation, you should call Abort() so that at least the client is closed. The service instance / session will still remain on the server (since there is no connection between them) and will remain so until the instance timed out. If you called Close() on a damaged channel, it would cause more errors.
  • Your service throws a FaultException . This does not mean that the communication channel will be put into a malfunctioning state. those. you can still make calls using the same client. And as such, in your example you should not call Abort() .

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

You can check the status of the communication channel using:

 ICommunicationObject comObj = ((ICommunicationObject)client); if(comObj.State == CommunicationState.Faulted) client.Abort(); else client.Close(); 
+5
source share

Have you tried this path, which I use to call 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(); } } } } 
+2
source share

All Articles