C# 如何从 ASMX Web 服务返回错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1153710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Return Errors from an ASMX Web Service?
提问by JL.
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>
采纳答案by CraigTP
Yes, this is possible.
是的,这是可能的。
What you'll need to look into is the SoapException class, and specifically the Detail propertyof 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 XmlNodeand 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:
来自 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.
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.
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 属性旨在提供与 SOAP 请求的 Body 元素相关的特定于应用程序的错误详细信息。根据 SOAP 规范,如果由于 SOAP 请求的 Body 元素导致客户端请求无法处理而发生错误,则必须设置 Detail 属性。如果 SOAP 请求的头条目中发生错误,则必须抛出 SoapHeaderException,以便在 SOAP 头中返回错误详细信息。如果错误没有发生,由于 Body 元素的处理,那么 Detail 属性一定不能设置。
在为 Detail 属性构建 XmlNode 时,DetailElementName 的 Name 和 Namespace 属性可用于确保与 SOAP 规范的一致性 [原文如此]。
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 SoapHeaderExceptionrather than a SoapException ifthe error occurs within the client's header section of the original XML request (this can often be the case when using custom SOAP headersfor e.g. security credentials) as detailed above.
请注意,如果您希望与您的 Web 服务响应保持正确的 SOAP 兼容,如果错误发生在原始 XML 请求的客户端标头部分中,您将需要返回SoapHeaderException而不是 SoapException (这通常是这种情况)使用自定义 SOAP 标头作为例如安全凭证),如上所述。