ASMX不会抛出SOAP错误

时间:2011-10-25 21:46:27

标签: asp.net web-services soap

我的ASP.NET 3.5 Web服务(asmx)不会将异常作为SOAP错误抛出。我见过的所有内容都说这是默认行为,但我的所有异常信息都是text / plain。这是一个新的Web应用程序项目,添加了一项服务。开箱即用的行为没有其他变化。我如何获得SOAP错误?

代码:

[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";
    }
}

结果:

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

1 个答案:

答案 0 :(得分:3)

如果请求是SOAP,ASMX Web服务将仅响应SOAP。服务为您生成的测试页面不包含SOAP请求信封:

POST http://server/service.asmx/HelloWorld HTTP/1.1
Accept: */*
Content-Type: application/x-www-form-urlencoded
Content-Length: 0
Host: server

正确的SOAP请求如下所示:

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>

注意SOAPAction头字段和SOAP信封内容。如果请求是有效的SOAP请求,则服务将使用SOAP响应进行响应。对于初始示例,以下SOAP错误是响应:

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. ---    &gt; 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>