首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在.Net web服务中引发SoapException

在.Net web服务中引发SoapException
EN

Stack Overflow用户
提问于 2013-07-03 21:14:37
回答 1查看 10.3K关注 0票数 4

编辑:我到处寻找这个问题的答案,似乎没有人得到类似的问题。在我看来,抛出SoapException应该根据需要格式化响应,而不仅仅是异常消息。感谢您给予的任何帮助。

我正在尝试返回如下所示的SoapException (示例):

代码语言:javascript
复制
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> 

为此,我实现了这样的代码,取自SoapException的MSDN站点

代码语言:javascript
复制
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

但是,当我运行此命令时,实际发送的响应是:

代码语言:javascript
复制
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

如何获得如下格式的响应:

代码语言:javascript
复制
<soap:Envelope>
    <soap:Body>
        <soap:Fault>
            <faultcode/>
            <faultstring/>
            <detail/>
        </soap:Fault>
    </soap:Body>
</soap:Envelope>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-07 18:30:01

您的代码应该可以工作,并根据MSDN示例给出一个格式化的错误,或者,如果您想要一个与您发布的响应示例相同的结果,那么像这样的服务应该可以做到这一点:

代码语言:javascript
复制
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

像这样的调用:

代码语言:javascript
复制
<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>

应返回以下内容:

代码语言:javascript
复制
<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>

您还需要将此代码添加到Web.config文件中,以删除错误字符串中的任何堆栈跟踪:

代码语言:javascript
复制
<configuration>
    <system.web>
        <customErrors mode="On" />
        ...
    ...     
...

而且,通常不需要手动构建SoapException,而是抛出更多适当的异常,并让ASP.NET将其包装在SoapFault中。更多细节请看这里:Using SOAP faults

使用SoapUI调用您的方法,您应该会得到上面的结果。确保在单击"Invoke“(例如http://localhost:8080/Service1.asmx/Process )时在SOAP端点(例如http://localhost:8080/Service1.asmx )上进行POST,而不是在测试页面的URL上,因为这不会返回SOAP格式的响应。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17449035

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档