Customizing the Fault contract

If you open the ProductFault.cs file in the FaultContracts project under the GeneratedCode folder, you will see that the ProductFault class doesn't have a constructor. To make it easier to throw a ProductFault exception in the product service, we will customize it to include a constructor with a string parameter as the fault message.

We can open the ProductFault.cs file and modify it directly, adding the constructor as needed. However, because this class is generated by Service Factory, any change to it will be lost if we ever need to regenerate it. For example, we may want to add a new member to the fault in the future, such as the feedback method for a specific fault, at which point we will have to regenerate it.

So we will add another file, called ProductFault.cs, but make it a partial class, to extend the generated ProductFault class.

Follow these steps to add the partial class:

  1. In the Solution Explorer, right-click on the project, MyWCF.EasyNorthwind.FaultContracts.
  2. Select Add | Class from the context menu.
  3. Add a new class file, named ProductFault.cs.

Then, customize the file as follows:

  • Change it to be a public partial class
  • Add a constructor with one string parameter

The content of this file should look like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyWCF.EasyNorthwind.FaultContracts
{
public partial class ProductFault
{
public ProductFault(string message)
{
this.faultMessage = message;
}
}
}

Again, build the FaultContracts project to make sure that there is no build error.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset