Throwing SoapException in .Net web service

前端 未结 1 1891
独厮守ぢ
独厮守ぢ 2021-01-13 17:47

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 SoapException should fo

1条回答
  •  無奈伤痛
    2021-01-13 18:09

    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:

    Imports System
    Imports System.Web.Services
    Imports System.Web.Services.Protocols
    Imports System.ComponentModel
    Imports System.Xml.Serialization
    Imports System.Xml
    
     _
     _
     _
    Public Class Service1
        Inherits WebService
    
        
        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:

    
       
       
          
       
    
    

    should return this:

    
       
          
             soap:Server
             BlahBlahBlahBlahBlah
             
          
       
    
    

    You also need to add this to your Web.config file to remove any stacktrace in your fault string:

    
        
            
            ...
        ...     
    ...
    

    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.

    Use SoapUI to 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.asmx and not on the URL of the test page when you click "Invoke" e.g. http://localhost:8080/Service1.asmx/Process as that does not return SOAP formatted responses.

    0 讨论(0)
提交回复
热议问题