如何从ASMX Web服务返回错误? [英] How to Return Errors from an ASMX Web Service?

查看:116
本文介绍了如何从ASMX Web服务返回错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

使用C#ASP.NET v2是否可以?

Is this possible using C# ASP.NET v2?

例如,

正常运行应返回:

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

但是我想出错

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

推荐答案

是的,这是可能的.

您需要查看的是

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

SoapException类将有效地呈现" Soap Fault ",即一种从Web服务方法向客户/消费者返回错误信息的符合标准的机制.

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类的属性的类型为 XmlNode ,因此可以包含单个节点/元素或子节点的层次结构.因此,Detail节点可以轻松地包含并充当父"节点.您自己的自定义错误对象的序列化表示形式.

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.

从MSDN:

Detail属性用于提供与SOAP请求的Body元素相关的应用程序特定的错误详细信息.根据SOAP规范,如果由于SOAP请求的Body元素而导致无法处理客户端请求而发生错误,则必须设置Detail属性.如果SOAP请求的标头条目中发生错误,则必须引发SoapHeaderException,以便在SOAP标头中返回错误详细信息.如果由于处理了Body元素而没有发生错误,那么就不能设置Detail属性.

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属性构建XmlNode时,可以使用DetailElementName的Name和Namespace属性来确保与SOAP规范的一致性.

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元素的所有直接子元素都称为detail条目,每个detail条目都被编码为detail元素内的独立元素.

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.

请注意,如果您希望对Web服务响应保持正确的SOAP兼容,则需要返回自定义SOAP标头的情况例如安全凭证).

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.

这篇关于如何从ASMX Web服务返回错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆