Validating WCF Service Messages with Validation Application Block - Part 2
This post is one of a post series about WCF Integration in Enterprise Library 3.0. Specifically, this post is the second post I am talking about the integration between Validation Application Block and Windows Communication Foundation (WCF). In the first post I talked about the kinds of parameters validation that can be used. In this post I'll talk about what happens when the parameters are not valid.
The Validation Application Block Integration with WCF is implemented by a Parameter Inspector. I'll talk about this in details in a later post, but for now I'll say that the Validation Application Block inspector intercepts calls to service operations and performs logic for validating the parameters passed to that operation. What happens if the parameters are found invalid is that the actual service operation is never get executed, and an exception (actually a Fault) is thrown back to the client.
Validation Results
When using the standard code to validate on object, we usually use the following code snippet. We validate the target object, receive back a ValidationResults object that contains the list of validation results that were returned by the validators.
Validator validator = ValidationFactory.CreateValidator<OrderData>();
ValidationResults results = validator.Validate(myOrderData);
foreach (ValidationResult result in results)
{
...
}
With WCF Services things are a little but different, since we do not validate the objects, but the Parameter Inspector does. If the parameters are found invalid, the Parameter Inspector throws a FaultException<ValidationFault> which is translated to a Soap Fault on the wire. The ValidationFault class (similar to ValidationResults) contains an array of ValidationDetails items which are similar to ValidationResult items that contain the validation message, key and tag.
The WCF Fault Handling is a basic subject that one should be familiar with and it is not part of this post, but what is important to mention is that if you want to be able to catch this exception in the client, and get the details about the validation errors, there are two things that you want to do:
1. Specify the FaultContract attribute foreach service operation that has a validation attached to it (or its parameters), and give the ValidationFault class as the type of the fault:
[ServiceContract]
public interface IOrdersService
{
[FaultContract(typeof(ValidationFault))]
[OperationContract]
OrderInfo CreateOrder(OrderData orderData);
}
2. Use the following code to catch the validation exception and investigate the validation details.
try
{
// Call the service operation
}
catch (FaultException<ValidationFault> ex)
{
// Extract the Detail node from the Fault Exception. This details is the
// ValidationFault class
ValidationFault fault = ex.Detail;
// Iterate through the list of validation errors
Console.WriteLine("Fault Occurred:");
foreach (ValidationDetail validationResult in faults.Details)
{
Console.WriteLine(string.Format("Message={0} Key={1} Tag={2}",
validationResult.Message, validationResult.Key, validationResult.Tag));
}
}
In this post I talked about what happens when the parameters are found to be invalid, and have shown the attribute and code that is required to be able to return an exception to the client and investigate it. In the next post I'll talk about how to plug in the Validation Application Block into WCF in order to integrate them.
Enjoy!