In all managed applications, processing errors are represented by Exception objects. In WCF applications, service methods communicate processing error information using SOAP fault messages. SOAP faults are message types that are included in the metadata for a service operation and, therefore, create a fault contract that clients can use to make their operation more interactive. In addition, because SOAP faults are expressed to clients in XML form, they are highly interoperable.

To be more concrete, let us start with an example:

Create an empty solution in VS2005 called "IndigoException". Add one project called "IndigoService". Next add a reference to System.ServiceModel and then add an interface.

using System;

using System.Collections.Generic;

using System.Text;

using System.ServiceModel;

using System.Runtime.Serialization;

namespace Indigo

{

[ServiceContract()]

publicinterfaceIService

{

[OperationContract]

[FaultContract(typeof(MyFaultException))]

string GetMessage();

}}

Add one more class as follows:

using System;

using System.Text;

using System.Runtime.Serialization;

namespace Indigo

{

[DataContract]

publicclassMyFaultException

{

privatestring _reason;

[DataMember]

publicstring Reason

{

get { return _reason; }

set { _reason = value; }

}

}

}

Add one class "Service" and a reference to System.ServiceModel

using System;

using System.Collections.Generic;

using System.Text;

using System.ServiceModel;

namespace Indigo

{

publicclassService : IService

{

#region IService Members

publicstring GetMessage()

{

try

{

int i, j, k;

j = 1;

k = 0;

i = j / k;

}

catch (Exception exp)

{

MyFaultException theFault = new MyFaultException();

theFault.Reason = "Some Error " + exp.Message.ToString();

thrownew FaultException<MyFaultException>(theFault);

}

return"No Error";

}

#endregion

}

}

Build the project.

Now add another project "Host" and add reference to "IndigoService" project and to System.ServiceModel