Reputation: 399
My ASP.NET 3.5 web service (asmx) is not throwing exceptions as SOAP faults. Everything I've seen says this is the default behavior but mine is sending all exception information as text/plain. This is a new web application project with one service added. No other changes from out of the box behavior. How do I get SOAP faults?
Code:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class WebService : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
int test1 = 0;
int test2 = 5 / test1;
return "Hello World";
}
}
Result:
HTTP/1.1 500 Internal Server Error
Server: ASP.NET Development Server/10.0.0.0
Date: Tue, 25 Oct 2011 21:39:23 GMT
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/plain; charset=utf-8
Content-Length: 201
Connection: Close
System.DivideByZeroException: Attempted to divide by zero.
at WebApplication.WebService.HelloWorld() in C:\....cs:line 23
Upvotes: 1
Views: 1647
Reputation: 399
ASMX web services will only respond with SOAP if the request was SOAP. The testing page generated for you by the service doesn't include the SOAP request envelope:
POST http://server/service.asmx/HelloWorld HTTP/1.1
Accept: */*
Content-Type: application/x-www-form-urlencoded
Content-Length: 0
Host: server
A proper SOAP request looks like this:
POST http://server/service.asmx HTTP/1.1
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://server/HelloWorld"
Host: server
Content-Length: 283
Expect: 100-continue
<?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>
<HelloWorld xmlns="http://server/" />
</soap:Body>
</soap:Envelope>
Notice the SOAPAction header field and the SOAP envelope content. If the request is a valid SOAP request the service will respond with a SOAP response. For the initial example, the following SOAP fault is the response:
HTTP/1.1 500 Internal Server Error
Date: Sat, 05 Nov 2011 16:55:17 GMT
Content-Type: text/xml; charset=utf-8
Content-Length: 695
<?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>
System.Web.Services.Protocols.SoapException: Server was unable to process request. --- > System.DivideByZeroException: Attempted to divide by zero.
at Service.HelloWorld() in C:\Test\Service.asmx.cs:line 35
--- End of inner exception stack trace ---
</faultstring>
<detail />
</soap:Fault>
</soap:Body>
</soap:Envelope>
Upvotes: 3