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.