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:
Then, customize the file as follows:
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.