WCF ValidationFault

1.9k views Asked by At

I'm using Validation Application Block - Enterprise Library to validate parameters sent to my WCF Service operations. For instance, a certain operation requires the parameter to either be a 1 or 6, like so:

[OperationContract(Name="GetEmployeesByRegion")]
[FaultContract(typeof(ValidationFault))]
List<Employees> GetEmployeesByRegion([DomainValidator(1,6)]int regionId);

This works just fine i.e the validation fault occurs however, when the service is invoked by the client, a generic System.ServiceModel.FaultException is thrown. An the message indicates: "The creator of this fault did not specify a reason."

Now, I could check the parameters myself before the service cal and throw a custom fault but that seems to defeat the purpose of attribute based validation of parameters using the Validation Application Block. Is there anyway to customize the error returned by the validation Fault? It is also possible I'm doing something completely wrong. I just want the caller to know that he/she should have passed in a 1 or 6 in the exception message. Is this possible?

2

There are 2 answers

0
badlife On

So this is ancient, but I was having the same problem and did find an answer.

The problem for me was that I added the [ValidationBehavior] and [FaultContract< ValidationFault>)] attributes after I created the reference to the service in my client. In order to get it working, i had to refresh the reference.

Some other possible things to check:

1) This was a bug that was fixed in Enterprise Library 6. I'm not sure about that one but have found a few mentions of code fixes to EntLib for similar problems. Make sure you're using the latest version of EntLib.

2) You are using the ErrorMessage parameter for your validation error message instead of MessageTemplate.

0
Kwal On

There is actually a collection that is present within the fault that has all the messages you are looking for:

foreach (ValidationDetail detail in fault.Detail.Details)
{
   ...
}