开发者

WCF and Validation application block: Impossible to customize error messages?

开发者 https://www.devze.com 2023-01-16 10:33 出处:网络
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?

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>
       开发者_Python百科          <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?


I think you need to use the MessageTemplate property instead of the ErrorMessage property. Try:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号