none
WCF Channel Faulting

    Question

  • In WCF I'm trying Channel Faulting feature. but I noticed that after throwing FaultException in the server side the state of the proxy is not changing to faulted. as well as I could use the same channel again without getting CommunicationObjectFaultedException

    //service code
    static int xx = 0;
    public void DoWork()
            {
                if (xx == 0)
                {
                    xx++;
                    throw new FaultException();
                }
    }

    //client code
    Service1Client x = new Service1Client();
                try
                {
                    x.DoWork();
                }
                catch { }        
    
                x.DoWork();

    Please advise






    Tuesday, August 27, 2013 10:19 PM

Answers

  • Hi,

    Please try to implement a fault contract then both your service and client knows this exception is expected. So when you throw a fault exception it will currpt the channel.

    Here is an example about using the fault contract:

    [ServiceContract]
    public interface IService
    {
       [OperationContract]
       [FaultContractAttribute(typeof(DatabaseFault))]
       string PostData(string msg);
    }
    
    [DataContract]
    public class DatabaseFault
    { 
      public DatabaseFault(exception e, string msg, string command)
      {
        this.ServerException = e;
        this.QueryParameter = msg;
        this.SqlCommand = command;
      }
    
      [DataMember]
      public Exception ServerException
      { get; set; }
    
      [DataMember]
      public string QueryParameter
      { get; set; }
    
      [DataMember]
      public string SqlCommand
      { get; set; }
    }
    
    public class Service : IService
    {
      public string PostData(string msg)
      {
         var command = "select * from db where col = msg";
         try
         {
            command.execute()
         }
         catch(DataException de)
         {
            throw new FaultException<DatabaseFault>(de, msg, command);
         }
      }
    }

    Best Regards,
    Amy Peng

    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Thursday, August 29, 2013 2:55 AM
    Moderator