WCF FaultException

sagesky36 picture sagesky36 · Feb 27, 2014 · Viewed 14.9k times · Source

When building a WCF service, if I don't include my contract for the FaultException in the service, I'm able to create a WCF service reference on the client with all of the members included in the Reference.cs file.

If I include my FaultException contract, two things happen.

  1. In the WCF test client, I have a red "X" next to the contract name (CreateFaultMessage)

  2. When I create the WCF service reference, not all of the members are included in the Reference.cs file.

I am hoping somebody will be able to inform me what I'm doing incorrectly as far as creating my FaultException contract since I am not a WCF expert.

Below, is the code declaration of my contract in the interface.

[OperationContract]
        [FaultContractAttribute(typeof(LogEventArgs), ProtectionLevel = ProtectionLevel.None)]
        Exception CreateFaultMessage(Exception ex, string method);

Below, are methods in the class which implement the interface. Notice the call to the CreateFaultMessage method in the catch block of the PostTransmissionRecord method.

public bool PostTransmissionRecord(TransmissionRecord transmissionRecord)
{
    bool blnUpdated = false;

    try
    {
        blnUpdated = TransmissionRecordGateway.InsertData(transmissionRecord);
        LogMessage.WriteEventLog("Transmission records updated successfully", "Ryder.ShopProcessService.SOA", 3, null);

        return blnUpdated;
    }
    catch (Exception ex)
    {
        LogMessage.WriteEventLog("Class: ShopProcessService" + CrLf + "Method: PostTransmissionRecord" + CrLf + "Error: " + ex.Message + CrLf + "InnerException: " + ex.InnerException + CrLf + "Source: " + ex.Source + CrLf + "StackTrace: " + ex.StackTrace, "Ryder.ShopProcessService.SOA", 1, null);
        IssueRemedyTicket("Class: ShopProcessService" + CrLf + "Method: PostTransmissionRecord" + CrLf + "Error: " + ex.Message + CrLf + "InnerException: " + ex.InnerException + CrLf + "Source: " + ex.Source + CrLf + "StackTrace: " + ex.StackTrace);
        CreateFaultMessage(ex, "UpdateSearchInventory");
        return blnUpdated;
    }
}

public Exception CreateFaultMessage(Exception ex, string method)
{
    LogEventArgs detail = new LogEventArgs();

    detail.ApplicationID = "ShopProcessService";
    detail.ExceptionMessage = ex.Message;
    detail.LogEventType = LogEventTypeEnum.Error;
    detail.Method = method;
    detail.Source = ex.Source;
    detail.StackTrace = ex.StackTrace;
    if (ex.InnerException != null)
        detail.InnerExceptionMessage = ex.InnerException.ToString();
    else
        detail.InnerExceptionMessage = null;

    throw new FaultException<LogEventArgs>(detail);
}

UPDATED CODE

DataContract class

namespace Ryder.Enterprise.DataTransferObjects
{
    [Serializable, DataContract(Name = "LogEventArgs", Namespace = "http://www.Ryder.com/SOA/DataContracts/2014/02/17")]
    public class LogEventArgs
    {
        private string applicationID;
        private string method;
        private LogEventTypeEnum logEventType;
        private string exceptionMessage;
        private string innerExceptionMessage;
        private string stackTrace;
        private string source;

        [DataMember(Name = "ApplicationID")]
        public string ApplicationID
        {
            get
            {
                return applicationID;
            }
            set
            {
                applicationID = value;
            }
        }
.
.
.

Service method throwing FAULTEXCEPTION

public bool UpdateSpotCheckInventory(SpotCheck transferObject)
        {
            bool blnUpdated = false;

            try
            {
                blnUpdated = SpotCheckCollectionGateway.UpdateData(transferObject);
                LogMessage.WriteEventLog("SpotCheck records updated successfully", "Ryder.ShopProcessService.SOA", 3, null);

                return blnUpdated;
            }
            catch (Exception ex)
            {
                //throw fault exception here on service
                return blnUpdated;
            }
        }

CLIENT CODE CATCHING THE FAULTEXCEPTION

catch (FaultException<LogEventArgs> ex)
            {                   ex.Detail.ApplicationID = "ShopProcessService";
                ex.Detail.LogEventType = LogEventTypeEnum.Error;
                serviceClient.Abort();
            } 

Answer

dhrumilap picture dhrumilap · Feb 28, 2014

Please try define the CustomFault(MathFault in this example) as a data contract rather than Operation Contract, like:

[DataContract]
public class MathFault
{    
    //...
}

At the client side:

static void Main(string[] args)
{
    ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
    try
    {
        int value1 = 22;
        int value2 = 0;
        int result = client.Divide(value1, value2);
        Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
        client.Close();
    }
    catch (FaultException<MathFault> e)
    {
        Console.WriteLine("FaultException<MathFault>: Math fault while doing " + e.Detail.Operation + ". Problem: " + e.Detail.ProblemType);
        client.Abort();
        Console.ReadLine();
    }
}

See more: Fault Contract