How to throw an exception with complex types or data contracts with a fault exception
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.
- Define the type to pass the details of SOAP faults as exceptions from a service back to a client
public class DatabaseFault
public string DbOperation;
public string DbReason
public string DbMessage;
- Use the FaultContract attribute in the ListCustomers method to generate SOAP faults.
public interface ICustomerService
// Get the list of customers
- 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.
DatabaseFault df = new DatabaseFault();
df.DbOperation = "ExecuteReader";
df.DbReason = "Exception in querying the Northwind database.";
df.DbMessage = e.Message;
throw new FaultException<DatabaseFault>(df);