如何自定义vb.net WCF和应用程序块中的错误消息?

2026-05-06 12:121阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计403个文字,预计阅读时间需要2分钟。

如何自定义vb.net WCF和应用程序块中的错误消息?

在将WCF与Enterprise Library 5.0的验证应用程序模块一起使用时,能否自定义发送回客户端的错误消息?

我已经将ValidationBehaviour属性添加到我的服务合同中,并将FaultContract属性添加到操作中:

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

我已将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="www.microsoft.com/practices/EnterpriseLibrary/2007/01/wcf/validation" xmlns:i="www.w3.org/2001/XMLSchema-instance"> <Details xmlns:a="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属性.尝试:

如何自定义vb.net WCF和应用程序块中的错误消息?

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

本文共计403个文字,预计阅读时间需要2分钟。

如何自定义vb.net WCF和应用程序块中的错误消息?

在将WCF与Enterprise Library 5.0的验证应用程序模块一起使用时,能否自定义发送回客户端的错误消息?

我已经将ValidationBehaviour属性添加到我的服务合同中,并将FaultContract属性添加到操作中:

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

我已将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="www.microsoft.com/practices/EnterpriseLibrary/2007/01/wcf/validation" xmlns:i="www.w3.org/2001/XMLSchema-instance"> <Details xmlns:a="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属性.尝试:

如何自定义vb.net WCF和应用程序块中的错误消息?

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