Search code examples
c#asp.netsoapasmx

Throwing a SoapException does not return a valid SOAP response


I'm working on an old web site. It is calling an ASP.Net web service (old .asmx files). Whenever an exception happens in the web service, it will throw a SoapException. The client catches this exception but is not able to pars it.

I'm viewing the SOAP request and response using both Fiddler and SOAP UI and reading this article http://msdn.microsoft.com/en-us/library/aa480514.aspx I expect to get the response in the following format:

<soap:Fault>
<faultcode>soap:Server</faultcode>    
<faultstring>Server was unable to process request. Something bad happened</faultstring>
<detail />

However, I'm just getting the following response:

HTTP/1.1 500 Internal Server Error
Server: ASP.NET Development Server/10.0.0.0
Date: Thu, 12 Jun 2014 05:27:14 GMT
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/plain; charset=utf-8
Content-Length: 164
Connection: Close

System.Web.Services.Protocols.SoapException: Something bad happened
   at WebService1.Service1.HelloWorld() in ...

Which I believe is the reason the client can't process the exception.

This happens even in a sample Hello World web service.

What can be wrong?

UPDATE: When I call my web service method that does NOT throw exception, I will get response in the following format:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ords="http://mydomain.com/">
   <soapenv:Header/>
   <soapenv:Body>
      ...
   </soapenv:Body>
</soapenv:Envelope>

This is how I throw the SoapException:

[WebMethod]
public string HelloWorld()
{
     throw new SoapException("Something bad happened", SoapException.ClientFaultCode);
     //return "Hello World";
}

Solution

  • I found some httpHandlers and modules in the web service web.config files that were not used at all. Removing those fixed the problem and now I'm getting the correct XML SOAP fault in the response.