This project is read-only.

How to throw an exception with complex types or data contracts with a fault exception

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

The following steps show an example of how to throw an exception with a data contract that has a complex type.
  1. Define the type to pass the details of SOAP faults as exceptions from a service back to a client
[DataContract]
public class DatabaseFault
{
  [DataMember]
   public string DbOperation;
   [DataMember]
   public string DbReason
   [DataMember]
   public string DbMessage;
}
  1. Use the FaultContract attribute in the ListCustomers method to generate SOAP faults.
[ServiceContract]
public interface ICustomerService
{
   // Get the list of customers
   [FaultContract(typeof(DatabaseFault))]
   [OperationContract]
   List<string> ListCustomers();
   …
}
  1. Create and populate the DatabaseFault object with the details of the exception in the Service implementation class and then throw a FaultException object with the DatabaseFault object details.
catch(Exception e)
{    
   DatabaseFault df = new DatabaseFault();
   df.DbOperation = "ExecuteReader";
   df.DbReason = "Exception in querying the Northwind database.";
   df.DbMessage = e.Message;
   throw new FaultException<DatabaseFault>(df);
}

Last edited May 8, 2008 at 4:39 AM by prashantbansode, version 1

Comments

No comments yet.