vb.net 在 .Net Web 服务中抛出 SoapException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17449035/
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
Throwing SoapException in .Net web service
提问by anothershrubery
EDIT: I have scoured high and low for an answer to this and nobody seems to be getting a similar issue. It seems to me that throwing the SoapExceptionshould format the response as required, not with just the exception message. Any help gratefully received.
编辑:我已经四处寻找答案,似乎没有人遇到类似的问题。在我看来,抛出SoapException应该根据需要格式化响应,而不仅仅是异常消息。感激地收到任何帮助。
I am trying to return a SoapExceptionthat should look something like this (example):
我正在尝试返回一个SoapException看起来像这样的(示例):
HTTP/1.1 500 Internal Server Error.
Date: Wed, 26 May 2004 05:12:08 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322
Cache-Control: private
Content-Type: text/xml; charset=utf-8
Content-Length: 488
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>BlahBlahBlahBlahBlah</faultstring>
<detail />
</soap:Fault>
</soap:Body>
</soap:Envelope>
For this I have implemented code like this, taken from the MSDN site for SoapException:
为此,我已经实现了这样的代码,取自 MSDN 站点SoapException:
Imports System
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Xml.Serialization
Imports System.Xml
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class Service1
Inherits System.Web.Services.WebService
<WebMethod()>
Public Sub Process()
' Build the detail element of the SOAP fault.
Dim doc As New System.Xml.XmlDocument()
Dim node As System.Xml.XmlNode = doc.CreateNode(XmlNodeType.Element, _
SoapException.DetailElementName.Name, _
SoapException.DetailElementName.Namespace)
' Build specific details for the SoapException.
' Add first child of detail XML element.
Dim details As System.Xml.XmlNode = doc.CreateNode(XmlNodeType.Element, _
"mySpecialInfo1", "http://tempuri.org/")
' Add second child of detail XML element with an attribute.
Dim details2 As System.Xml.XmlNode = doc.CreateNode(XmlNodeType.Element, _
"mySpecialInfo2", "http://tempuri.org/")
Dim attr As XmlAttribute = doc.CreateAttribute("t", "attrName", _
"http://tempuri.org/")
attr.Value = "attrValue"
details2.Attributes.Append(attr)
' Append the two child elements to the detail node.
node.AppendChild(details)
node.AppendChild(details2)
'Throw the exception
Dim se As New SoapException("Fault occurred", SoapException.ClientFaultCode, _
Context.Request.Url.AbsoluteUri, node)
Throw se
Return
End Sub
End Class
However when I run this, the actual response sent is:
但是,当我运行它时,发送的实际响应是:
HTTP/1.1 500 Internal Server Error
Server: ASP.NET Development Server/10.0.0.0
Date: Wed, 03 Jul 2013 13:06:26 GMT
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/plain; charset=utf-8
Content-Length: 233
Connection: Close
System.Web.Services.Protocols.SoapException: Fault occurred
at MyService.Service1.Process() in C:\MyLocation\MyService\Service1.asmx.vb:line 42
How do I get the response formatted like:
我如何获得格式如下的响应:
<soap:Envelope>
<soap:Body>
<soap:Fault>
<faultcode/>
<faultstring/>
<detail/>
</soap:Fault>
</soap:Body>
</soap:Envelope>
采纳答案by Bogdan
Your code should work and give you a formatted fault as per the MSDN example or, if you want a result as in the response sample you posted, then a service like this should do the trick:
您的代码应该可以工作,并按照 MSDN 示例为您提供格式化的错误,或者,如果您想要像您发布的响应示例一样的结果,那么像这样的服务应该可以解决问题:
Imports System
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Xml.Serialization
Imports System.Xml
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class Service1
Inherits WebService
<WebMethod()>
Public Sub Process()
Dim detailsNode As XmlNode = Nothing
Dim actorString As String = Nothing
Throw New SoapException("BlahBlahBlahBlahBlah", SoapException.ServerFaultCode, actorString, detailsNode)
End Sub
End Class
A call like this:
像这样的调用:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:Process/>
</soapenv:Body>
</soapenv:Envelope>
should return this:
应该返回这个:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>BlahBlahBlahBlahBlah</faultstring>
<detail/>
</soap:Fault>
</soap:Body>
</soap:Envelope>
You also need to add this to your Web.configfile to remove any stacktrace in your fault string:
您还需要将此添加到您的Web.config文件中以删除故障字符串中的任何堆栈跟踪:
<configuration>
<system.web>
<customErrors mode="On" />
...
...
...
Also, it's usually not necessary to build the SoapException by hand but throw more appropriate exceptions and let ASP.NET wrap it in a SoapFault. See here for more details: Using SOAP faults.
此外,通常不需要手动构建 SoapException,而是抛出更合适的异常并让 ASP.NET 将其包装在 SoapFault 中。有关更多详细信息,请参见此处:使用 SOAP 错误。
Use SoapUIto call your method and you should get the above result. Make sure you make a POST on the SOAP endpoint e.g. http://localhost:8080/Service1.asmxand not on the URL of the test page when you click "Invoke" e.g. http://localhost:8080/Service1.asmx/Processas that does not return SOAP formatted responses.
使用SoapUI调用你的方法,你应该得到上面的结果。http://localhost:8080/Service1.asmx当您单击“调用”时,请确保在 SOAP 端点(例如)上而不是在测试页面的 URL上进行 POST,例如http://localhost:8080/Service1.asmx/Process因为这不会返回 SOAP 格式的响应。

