I am having bit of trouble returning custom fault exception from wcf service. The client application talking to wcf service gets – contract mismatch error. Here is my fault contract defined in the service:
public partial class Fault
{
string codeField;
string messageField;
string detailsField;
}
On error in the service I am creating custom fault exception as following:
public void ProvideFault(Exception exception, MessageVersion version, ref Message refMessage)
{
mynamespace.Fault customFault = new mynamespace.Fault()
{
code = "Service Error",
message = "Error Message",
details = "Error Message"
};
FaultException<mynamespace.Fault> faultexception = new FaultException<mynamespace.Fault>( customFault );
MessageFault messageFault = faultexception.CreateMessageFault();
refMessage = Message.CreateMessage( version, messageFault, faultNamespace );
}
This code causes contract mismatch error in client. Can anyone please help me finding what is wrong?
To send an undeclared SOAP fault, throw a System. ServiceModel. FaultException object (that is, not the generic type FaultException<TDetail>) and pass the string to the constructor. This is exposed to the WCF client applications as a thrown System.
In a service, use the FaultException class to create an untyped fault to return to the client for debugging purposes. In a client, catch FaultException objects to handle unknown or generic faults, such as those returned by a service with the IncludeExceptionDetailInFaults property set to true .
Exceptions are used to communicate errors locally within the service or the client implementation. Faults, on the other hand, are used to communicate errors across service boundaries, such as from the server to the client or vice versa.
Specify what type of fault you need to return (for example, create a UserMismatchFault class), then throw it like so:
throw new FaultException<UserMismatchFault>(myFault);
Steps to create a custom fault contract
http://bloggingabout.net/blogs/jpsmit/archive/2007/03/21/wcf-fault-contracts.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With