WCF Callback channel faulted

15,077

so far I have experienced that a WCF connection needs to be recreated on fault - there doesn't seem to be a way to recover it otherwise. As for when a fault occurs, the method seems to fire fine, but often it fires and cleans up the WCF connection (establishing a new one, etc) as the current request is going through - causing this to fail - especially true on timeouts.

A couple of suggestions: - If it is timeout related, keep track of the last time a call was made and a constant containing the timeout value. If the WCF connection will have been dropped due to inactivity, drop it and recreate it before you send the request over the wire. - The other thing, it looks like you are not re-adding the fault handler, which means the first fault will get handled, but the second time it faults it will fall over without a handler cause no new one has been attached.

Hope this helps

Share:
15,077
phatoni
Author by

phatoni

Former C# Developer and now mostly focused on IT Consulting.

Updated on June 15, 2022

Comments

  • phatoni
    phatoni almost 2 years

    I'm trying to implement a reconnect logic for a wcf client. I'm aware that you have to create a new channel after the current channel entered the faulted state. I did this in a channel faulted event handler:

    
    internal class ServiceClient : DuplexClientBase, IServiceClient
    {
      public ServiceClient(ICallback callback, EndpointAddress serviceAddress)
       : base(callback, MyUtility.GetServiceBinding("NetTcpBinding"), serviceAddress)
      {
       // Open the connection.
       Open();
      }
    
      public void Register(string clientName)
      {
        // register to service
      }
    
      public void DoSomething()
      {
        // some code
      }
     }
    
     public class ClientApp
     {
      private IServiceClient mServiceClient;
    
      private ICallback mCallback;
    
      public ClientApp()
      {
       mServiceClient = new ServiceClient( mCallback, new EndpointAddress("someAddress"));
    
       mServiceClient.Register();
    
       // register faulted event for the  service client
       ((ICommunicationObject)mServiceClient).Faulted += new EventHandler(ServiceClient_Faulted);
      }
    
      void ServiceClient_Faulted(object sender, EventArgs e)
      {
       // Create new Service Client.
       mServiceClient = new ServiceClient( mCallback, new EndpointAddress("someAddress"));
    
       // Register the EI at Cell Controller
       mServiceClient.Register();
      }
    
      public void DoSomething()
      {
       mServiceClient.DoSomething();
      }
     }
    

    But in my unit test I still get a "The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state" exception.

    Is it possible that the callback channel is still faulted and if yes how can I replace the callback channel?

  • phatoni
    phatoni over 14 years
    Thanks for your help. Yes, I tried this before, but it did not help so much because I get an "The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Aborted state" exception instead of the above mentioned exception.