WCF and Validation application block: Customizing ValidationFault not possible?

Topics: Validation Application Block
Sep 13, 2010 at 2:28 PM

When using WCF together with Enterprise Library 5.0's validation application block, is it possible to customize the error messages which are sent back to the client?
I've added the ValidationBehaviour-attribute to my service contract and the FaultContract-attribute to the operation:

<ServiceContract()> _
<ValidationBehavior()> _
Public Interface IContract

    <OperationContract(), FaultContract(GetType(ValidationFault))> _
    Function GetCustomers(ByVal criteria As CustomersSearchCriteria) As List(Of Customer)

End Interface
 Then, inside the CustomersSearchCriteria I've added the validation attributes:
<Validators.StringLengthValidator(1, 3, ErrorMessage:="here's my error", Tag:="551")> _
Public Property Pin() As String
    Get
        Return _pin
    End Get
    Set(ByVal value As String)
        _pin = value
    End Set
End Property

 

But when called with invalid parameter, the returned SOAP looks like this:

 

      <s:Fault>
         <faultcode>s:Client</faultcode>
         <faultstring xml:lang="fi-FI">The creator of this fault did not specify a Reason.</faultstring>
         <detail>
            <ValidationFault xmlns="http://www.microsoft.com/practices/EnterpriseLibrary/2007/01/wcf/validation" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
               <Details xmlns:a="http://schemas.datacontract.org/2004/07/Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WCF">
                  <a:ValidationDetail>
                     <a:Key>Pin</a:Key>
                     <a:Message>The length of the value must fall within the range "1" (Inclusive) - "3" (Inclusive).</a:Message>
                     <a:Tag>criteria</a:Tag>
                  </a:ValidationDetail>
               </Details>
            </ValidationFault>
         </detail>
      </s:Fault>

The service returns ValidationFault as it should, but instead of having the error message and tag which I've defined, it uses some generic values. Is this a bug or am I doing something wrong?

Sep 14, 2010 at 1:31 AM

That is the current behavior, but not a bug.  You are supposed to really get the error message from the ValidationDetails.  It's currently not configurable and if you want to change it, you would have to modify the source code.  You can also vote for this workitem  in the Issue Tracker requesting it to be configurable.

 

Sarah Urmeneta
Global Technology & Solutions
Avande, Inc.
entlib.support@avanade.com

Jul 26, 2011 at 6:17 AM

Hi,

Change the following code ,

<Validators.StringLengthValidator(1, 3, ErrorMessage:="here's my error", Tag:="551")> _ 

To like below, (remove ErrorMessage and add MessageTemplate )

    [Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator(1, RangeBoundaryType.Inclusive,
            20, RangeBoundaryType.Inclusive,MessageTemplate = "here's my error")]

This will work fine.
Jan 31, 2012 at 5:41 AM
AvanadeSupport wrote:

That is the current behavior, but not a bug.  

 

Sarah Urmeneta
Global Technology & Solutions
Avande, Inc.
entlib.support@avanade.com

I got "The creator of this fault did not specify a Reason." when i try to use it

Feb 1, 2012 at 1:19 AM

Unfortunately, as per the workitem referenced above, that is the message that is set by default.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com