4
将WCF与Enterprise Library 5.0的验证应用程序块一起使用时,是否可以自定义发送回客户端的错误消息?WCF和验证应用程序块:无法自定义错误消息?
我已经添加了ValidationBehaviour属性我的服务合同和FaultContract属性的操作:
<ServiceContract()> _
<ValidationBehavior()> _
Public Interface IContract
<OperationContract(), FaultContract(GetType(ValidationFault))> _
Function GetCustomers(ByVal criteria As CustomersSearchCriteria) As List(Of Customer)
End Interface
然后,CustomersSearchCriteria里面我已经添加了验证属性:
<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
但是,当使用无效参数调用时,返回的SOAP如下所示:
<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>
该服务应该返回ValidationFault,但它不使用我定义的错误消息和标记,而是使用一些通用值。这是一个错误还是我做错了什么?
Tuzo是绝对正确的。 'ErrorMessage'是由DataAnnotations定义的属性。 VAB 5.0中的属性现在继承自DataAnnotations的属性,因此也具有此属性。我不明白他们为什么不允许用户也使用'ErrorMessage'属性。 – Steven 2010-09-13 20:59:24
@Steven,我同意你的看法--EL 5.0的实现很混乱。我唯一的猜测是他们想要向后兼容。 – 2010-09-13 23:43:37