Throwing a SoapException in the .NET web service.

EDIT: I scored a maximum and a minimum to answer this question, and no one seems to have encountered a similar problem. It seems to me that throwing a SoapException should format the response as needed, and not just with an exception message. Any help gratefully received.

I am trying to SoapException that should look something like this (example):

 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> 

To do this, I implemented the following code taken from the MSDN website for 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 is sent:

 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 to get an answer formatted as:

 <soap:Envelope> <soap:Body> <soap:Fault> <faultcode/> <faultstring/> <detail/> </soap:Fault> </soap:Body> </soap:Envelope> 
+4
source share
1 answer

Your code should work and give you a formatted error in accordance with the MSDN example or, if you want to get the result, as in the answer to the sample that you posted, then this service should do the trick:

 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 

Call:

 <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.config file to remove any stack in your error line:

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

In addition, you usually do not need to throw a SoapException manually, but throw more appropriate exceptions and let ASP.NET port it to SoapFault. See here Using SOAP Errors for more details.

Use SoapUI to call your method, and you should get the above result. Make sure you do a POST on the SOAP endpoint, for example. http://localhost:8080/Service1.asmx , not the URL of the test page when you click Call, for example. http://localhost:8080/Service1.asmx/Process , as this does not return formatted SOAP responses.

+1
source

Source: https://habr.com/ru/post/1489496/


All Articles