【问题标题】:ASMX not throwing SOAP faultsASMX 不抛出 SOAP 错误
【发布时间】:2011-10-25 21:46:27
【问题描述】:

我的 ASP.NET 3.5 Web 服务 (asmx) 没有将异常作为 SOAP 错误抛出。我所看到的一切都说这是默认行为,但我的是将所有异常信息作为文本/纯文本发送。这是一个添加了一项服务的新 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

【问题讨论】:

    标签: asp.net web-services soap


    【解决方案1】:

    如果请求是 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>
    

    【讨论】:

      猜你喜欢
      • 2016-08-21
      • 1970-01-01
      • 2017-06-20
      • 1970-01-01
      • 1970-01-01
      • 2018-05-26
      • 1970-01-01
      • 1970-01-01
      • 2015-02-23
      相关资源
      最近更新 更多