Sourabh
Sourabh

Reputation: 86

How to include actual Exception or Custom Exception in FaultContract of WCF

I would like to Include a User Define Exception in FaultContract of WCF. In my WCF application, I would like to encapsulate Exception instance/UserDefine Exception Instance in FaultContract. Please Find my below UserDefine Exception.

public class UserExceptions : Exception
{
    public string customMessage { get; set; }

    public string Result { get; set; }

    public UserExceptions(Exception ex):base(ex.Message,ex.InnerException)
    {

    }
}

public class RecordNotFoundException : UserExceptions
{
    public RecordNotFoundException(Exception ex): base(ex)
    {

    }
}

public class StoredProcNotFoundException : UserExceptions
{
    public string innerExp { get; set; }

    public StoredProcNotFoundException(Exception ex,string innerExp)
        : base(ex)
    {
        this.innerExp = innerExp;
    }
}

[DataContract]
public class ExceptionFault
{ 
    [DataMember]
    public UserExceptions Exception { get; set; }

    public ExceptionFault(UserExceptions ex)
    {
        this.Exception = ex;
    }
}

And I am throw Exception in service as below

try
        {
            //Some Code
            //Coding Section 
                    throw new RecordNotFoundException(new Exception("Record Not Found"));
            //Coding Section
        }
        catch (RecordNotFoundException rex)
        {
            ExceptionFault ef = new ExceptionFault(rex);
            throw new FaultException<ExceptionFault>(ef,new FaultReason(rex.Message));
        }
        catch (Exception ex)
        {
            throw new FaultException<ExceptionFault>(new ExceptionFault((UserExceptions)ex),new FaultReason(ex.Message));
        }

try block catch CustomException(RecordNotFoundException) but it is not able to send that exception to client.

Upvotes: 0

Views: 1243

Answers (1)

Glen Thomas
Glen Thomas

Reputation: 10744

You need to add the FaultContract attribute to your OperationContract methods so that the SOAP client knows to expect the exception type

[OperationContract]
[FaultContract(typeof(MathFault))]
int Divide(int n1, int n2);

Your catch block needs to catch FaultException<T>

catch (FaultException<MathFault> e)
{
    Console.WriteLine("FaultException<MathFault>: Math fault while doing " + e.Detail.operation + ". Problem: " + e.Detail.problemType);
    client.Abort();
}

Its best to have a DataContract for each exception type, rather than trying to wrap them all up into one DataContract

[DataContract]
public class MathFault
{    
    private string operation;
    private string problemType;

    [DataMember]
    public string Operation
    {
        get { return operation; }
        set { operation = value; }
    }

    [DataMember]        
    public string ProblemType
    {
        get { return problemType; }
        set { problemType = value; }
    }
}

If you want to include implementations of UserExceptions in your DataContract then you may need to use the KnownType attribute so that the SOAP client is aware of the types:

[DataContract]
[KnownType(typeof(RecordNotFoundException))]
[KnownType(typeof(StoredProcNotFoundException))]
public class ExceptionFault
{ 
    [DataMember]
    public UserExceptions Exception { get; set; }

    public ExceptionFault(UserExceptions ex)
    {
        this.Exception = ex;
    }
}

Upvotes: 0

Related Questions