gpt4 book ai didi

vb.net - WCF 和验证应用程序 block : Impossible to customize error messages?

转载 作者:行者123 更新时间:2023-12-02 06:31:12 24 4
gpt4 key购买 nike

当将WCF与Enterprise Library 5.0的验证应用程序 block 一起使用时,是否可以自定义发送回客户端的错误消息?

我已将 ValidationBehaviour 属性添加到我的服务契约(Contract)中,并将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,但它没有使用我定义的错误消息和标记,而是使用一些通用值。这是一个错误还是我做错了什么?

最佳答案

我认为您需要使用 MessageTemplate 属性而不是 ErrorMessage 属性。尝试:

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

关于vb.net - WCF 和验证应用程序 block : Impossible to customize error messages?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3700875/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com