Reputation: 83
In WCF service I would like to handle errors and exception using custom fault.
I need to be able in case of the error return
CustomFault with Description and Error code
How can I implement it?
Upvotes: 0
Views: 152
Reputation: 3301
First you need to create a DataContract class for your custom fault. It sounds like at a minimum in your case, this class will have an Error Code property and a Description property. Next, in your Service Contract, you'll need to decorate any service operations that can raise this fault with the FaultContract attribute. For example:
[OperationContract()]
[FaultContract(typeof(MyCustomFault))]
ResponseDataContract SomeServiceOperation(RequestDataContract request);
Finally, in your service implementation, you'll need to throw the custom fault as a FaultExcepton. For example:
try
{
DoStuff();
}
catch (Exception e)
{
throw new FaultException<MyCustomFault>(new MyCustomFault
{
Description = "Oh No!",
ErrorCode = 1234
});
}
Upvotes: 2