2013-07-03 58 views
4

編輯:我已經搜索了高和低的答案,沒有人似乎得到類似的問題。在我看來,拋出SoapException應根據需要設置響應的格式,而不僅僅是異常消息。任何幫助感激地收到。在.Net web服務中拋出SoapException

我試圖返回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> 

爲此,我已經實現了這樣的代碼,從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 

然而,當我運行這個,實際發送的迴應是:

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 

如何得到響應格式如下:

<soap:Envelope> 
    <soap:Body> 
     <soap:Fault> 
      <faultcode/> 
      <faultstring/> 
      <detail/> 
     </soap:Fault> 
    </soap:Body> 
</soap:Envelope> 

回答

1

您的代碼應該工作,並給你一個格式化的故障按照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 

像這樣的電話:

<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> 

應該返回此:

<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文件,刪除任何堆棧跟蹤你的錯誤字符串:

<configuration> 
    <system.web> 
     <customErrors mode="On" /> 
     ... 
    ...  
... 

此外,它通常沒有必要用手工打造的SoapException但拋出更合適的異常,讓ASP.NET將它包裝在SoapFault中。詳情請看這裏:Using SOAP faults

使用SoapUI來調用你的方法,你應該得到上面的結果。確保您在SOAP端點上進行POST,例如http://localhost:8080/Service1.asmx而不是在單擊「調用」時的測試頁的URL例如http://localhost:8080/Service1.asmx/Process,因爲它不會返回SOAP格式的響應。

+0

我正在使用Fiddler(http://www.fiddler2.com)來跟蹤請求和響應,它們如問題所示。在web.config中添加' On「/>'會刪除堆棧跟蹤,但是響應的內容完全是'發生故障',即。仍然沒有用肥皂標籤格式化。我首先使用鏈接提供作爲我的問題的基礎。 – anothershrubery

+0

@anothershrubery:查看我更新的答案。該通話使用了什麼終端?直接在asmx URL上使用SoapUI,你應該得到SOAP錯誤。 – Bogdan

+0

謝謝,我使用了SoapUI並看到了正確的格式。我認爲像Fiddler這樣的調試代理會傳回確切的請求/響應?很明顯不是。所以我一直在傳遞正確的東西,只是沒有用正確的東西去看它。乾杯。 – anothershrubery