I'm writing a WCF service for the first time. The service and all of its clients (at least for now) are written in C#. The service has to do a lot of input validation on the data it gets passed, so I need to have some way to indicate invalid data back to the client. I've been reading a lot about faults and exceptions, wrapping exceptions in faults, and a lot of conflicting articles that are just confusing me further. What is the proper way to handle this case?
Should I avoid exceptions altogether and package a Results return message? Should I create a special Fault, or a special Exception, or just throw ArgumentExceptions like I would for a non-WCF validation function?
The code I have right now (influenced by MSDN) is:
[DataContract]
public class ValidationFault
{
[DataMember]
public Dictionary<string, string> Errors { get; private set; }
[DataMember]
public bool Fatal { get; private set; }
[DataMember]
public Guid SeriesIdentifier { get; private set; }
public ValidationFault(Guid id, string argument, string error, bool fatal)
{
SeriesIdentifier = id;
Errors = new Dictionary<string, string> {{argument, error}};
Fatal = fatal;
}
public void AddError(string argument, string error, bool fatal)
{
Errors.Add(argument, error);
Fatal |= fatal;
}
}
And on the method there's [FaultContract(typeof(ValidationFault))]. So is this the "right" way to approach this?
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 .
In a WCF service, if it throws an exception inside the service, the client will not get the details. In order to get the formatted exception details on client side, you need to use FaultException instead to let the client know the details. The FaultException information can be serialized as expected.
Using FaultExceptionFault exceptions are exceptions that are thrown by a WCF service when an exception occurs at runtime -- such exceptions are typically used to transmit untyped fault data to the service consumers.
FaultContract inherits from the FaultContractAttribute Class. Create a table named EMPLOYEE with column names EMP_ID and EMP_NAME. We will create a service to input the user id and get the name of the employee. Step 2: Open Visual Studio then select "File" -> "New" -> "Project..." -> "WCF" -> "WCF Service Application".
Throwing an exception is not useful from a WCF service Why not? Because it comes back as a bare fault and you need to
a) Set the fault to include exceptions
b) Parse the fault to get the text of the exception and see what happened.
So yes you need a fault rather than an exception. I would, in your case, create a custom fault which contains a list of the fields that failed the validation as part of the fault contract.
Note that WCF does fun things with dictionaries, which aren't ISerializable; it has special handling, so check the message coming back looks good over the wire; if not it's back to arrays for you.
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