This project is read-only.

How to Implement a Data Contract To Propagate Exception Details for Debugging Purposes

- J.D. Meier, Carlos Farre, Jason Taylor, Prashant Bansode, Steve Gregersen, Madhu Sundararajan, Rob Boucher

The following steps shows how to implement a data contract to propagate exception details for debugging purposes
  1. Create a DataContract, with a member for fault reason
    public class MyDCFaultException
        private string _reason;

        public string Reason
            get { return _reason; }
            set { _reason = value; }
  1. Create a Service contract, specifiying a FaultContract with the above DataContract type for an operation
    public interface IService
        string DoSomeComplexWork();
  1. Implement the service operation. If there are any error in the service operation send the exception details by throwing the FaultException of type MyDCFaultException as below
    public class Service : IService
        public string DoSomeComplexWork()
                // some complex operations
            catch (Exception exp)
                MyDCFaultException theFault = new MyDCFaultException();
                theFault.Reason = "Some Error " + exp.Message.ToString();
                throw new FaultException<MyDCFaultException>(theFault);
            return "No Error";
  1. Call the service operation from a client application. Get the original service exception reason by using the Detail.Reason property.
                localhost.IService proxy = new localhost.ServiceClient();
                result = proxy.DoSomeComplexWork();

            catch (FaultException<localhost.MyDCFaultException> ex)
               result = "Exception: " + ex.Detail.Reason;


You can also send managed exception information to the clients using the IncludeExceptionDetailInFaults property in the serviceDebug element of your service behavior. By default its value is false, you can change it to true for debugging or problem diagnosis scenarios.
        <service name="MyService"
            <behavior name="MyServiceBehavior">
*                <serviceDebug includeExceptionDetailInFaults="true"/>*

Additional Resources

Last edited Jun 13, 2008 at 7:45 PM by prashantbansode, version 1


No comments yet.