如何从ASMX Web服务返回错误?

时间:2022-10-30 23:23:18

My web service method returns a collection object, this will serialize nicely, thanks to the way C# web services work!

我的Web服务方法返回一个集合对象,这将很好地序列化,这要归功于C#web服务的工作方式!

But if my code throws an uncaught exception, I want to instead return a custom error object.

但是,如果我的代码抛出未捕获的异常,我想返回一个自定义错误对象。

Is this possible using C# ASP.NET v2?

这可能是使用C#ASP.NET v2吗?

For example,

Normal Operation should return:

正常操作应返回:

<Books>
    <book>Sample</book>
    <book>Sample</book>
</Books>

But on error I want

但是我想要的错误

  <error>
      <errorMessage></errorMessage>
  </error>

1 个解决方案

#1


Yes, this is possible.

是的,这是可能的。

What you'll need to look into is the SoapException class, and specifically the Detail property of the SoapException class.

您需要了解的是SoapException类,特别是SoapException类的Detail属性。

The SoapException class will effectively render a "Soap Fault", which is the standards-compliant mechanism for returning error information to clients/consumers from a web service method.

SoapException类将有效地呈现“Soap Fault”,这是一种符合标准的机制,用于从Web服务方法向客户端/使用者返回错误信息。

The "Detail" property of the SoapException class is of type XmlNode and can thus contain either a single node/element or a hierarchy of child nodes. The Detail node could therefore easily contain and act as the "parent" for the serialized representation of your own custom error object.

SoapException类的“Detail”属性是XmlNode类型,因此可以包含单个节点/元素或子节点的层次结构。因此,Detail节点可以轻松地包含并充当您自己的自定义错误对象的序列化表示的“父”。

From MSDN:

The Detail property is intended for supplying application specific error details related to the Body element of the SOAP request. According to the SOAP specification, if an an error occurrs because the client request could not be processed due to the Body element of the SOAP request, the Detail property must be set. If an error occured in the header entries of the SOAP request, you must throw a SoapHeaderException, so that the error details are returned in the SOAP header. If the error did not occur, due to the processing of the Body element, then the Detail property must not be set.

Detail属性用于提供与SOAP请求的Body元素相关的特定于应用程序的错误详细信息。根据SOAP规范,如果由于SOAP请求的Body元素而无法处理客户端请求而发生错误,则必须设置Detail属性。如果SOAP请求的标头条目中发生错误,则必须抛出SoapHeaderException,以便在SOAP标头中返回错误详细信息。如果由于处理Body元素而未发生错误,则不得设置Detail属性。

In building an XmlNode for the Detail property, the Name and Namespace properties of DetailElementName can be used to ensure consistancy [sic] with the SOAP specification.

在为Detail属性构建XmlNode时,DetailElementName的Name和Namespace属性可用于确保与SOAP规范的一致性[sic]。

All immediate child elements of the detail element are called detail entries and each detail entry is encoded as an independent element within the detail element.

detail元素的所有直接子元素都称为detail条目,每个detail条目都被编码为detail元素中的独立元素。

Note that if you wish to remain correctly SOAP compliant with your web service responses, you'll need to return a SoapHeaderException rather than a SoapException if the error occurs within the client's header section of the original XML request (this can often be the case when using custom SOAP headers for e.g. security credentials) as detailed above.

请注意,如果您希望保持与Web服务响应的SOAP兼容,那么如果错误发生在原始XML请求的客户端头部分中,则需要返回SoapHeaderException而不是SoapException(这通常是这种情况。如上所述,使用自定义SOAP标头作为安全凭证。

#1


Yes, this is possible.

是的,这是可能的。

What you'll need to look into is the SoapException class, and specifically the Detail property of the SoapException class.

您需要了解的是SoapException类,特别是SoapException类的Detail属性。

The SoapException class will effectively render a "Soap Fault", which is the standards-compliant mechanism for returning error information to clients/consumers from a web service method.

SoapException类将有效地呈现“Soap Fault”,这是一种符合标准的机制,用于从Web服务方法向客户端/使用者返回错误信息。

The "Detail" property of the SoapException class is of type XmlNode and can thus contain either a single node/element or a hierarchy of child nodes. The Detail node could therefore easily contain and act as the "parent" for the serialized representation of your own custom error object.

SoapException类的“Detail”属性是XmlNode类型,因此可以包含单个节点/元素或子节点的层次结构。因此,Detail节点可以轻松地包含并充当您自己的自定义错误对象的序列化表示的“父”。

From MSDN:

The Detail property is intended for supplying application specific error details related to the Body element of the SOAP request. According to the SOAP specification, if an an error occurrs because the client request could not be processed due to the Body element of the SOAP request, the Detail property must be set. If an error occured in the header entries of the SOAP request, you must throw a SoapHeaderException, so that the error details are returned in the SOAP header. If the error did not occur, due to the processing of the Body element, then the Detail property must not be set.

Detail属性用于提供与SOAP请求的Body元素相关的特定于应用程序的错误详细信息。根据SOAP规范,如果由于SOAP请求的Body元素而无法处理客户端请求而发生错误,则必须设置Detail属性。如果SOAP请求的标头条目中发生错误,则必须抛出SoapHeaderException,以便在SOAP标头中返回错误详细信息。如果由于处理Body元素而未发生错误,则不得设置Detail属性。

In building an XmlNode for the Detail property, the Name and Namespace properties of DetailElementName can be used to ensure consistancy [sic] with the SOAP specification.

在为Detail属性构建XmlNode时,DetailElementName的Name和Namespace属性可用于确保与SOAP规范的一致性[sic]。

All immediate child elements of the detail element are called detail entries and each detail entry is encoded as an independent element within the detail element.

detail元素的所有直接子元素都称为detail条目,每个detail条目都被编码为detail元素中的独立元素。

Note that if you wish to remain correctly SOAP compliant with your web service responses, you'll need to return a SoapHeaderException rather than a SoapException if the error occurs within the client's header section of the original XML request (this can often be the case when using custom SOAP headers for e.g. security credentials) as detailed above.

请注意,如果您希望保持与Web服务响应的SOAP兼容,那么如果错误发生在原始XML请求的客户端头部分中,则需要返回SoapHeaderException而不是SoapException(这通常是这种情况。如上所述,使用自定义SOAP标头作为安全凭证。