当 .asmx处理程序由 HTTP 管道调用之后,它会通过查看在 .asmx 文件中发现的 WebService 声明来确定要检查哪个 .NET 类。然后,它会查看传入的 HTTP 消息中的信息,以便正确地确定要在被引用类中调用哪种方法。要调用先前示例中显示的 Add 操作,传入的 HTTP 消息必须具有如下所示的外观:

- POST /math/math.asmx HTTP/1.1
 - Host: localhost
 - Content-Type: text/xml; charset=utf-8
 - Content-Length: length
 - SOAPAction: "http://tempuri.org/Add"
 - < soap:Envelope
 - xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
 - >
 - < soap:Body>
 - < Add xmlns="http://tempuri.org/">
 - < x>33< /x>
 - < y>66< /y>
 - < /Add>
 - < /soap:Body>
 - < /soap:Envelope>
 
在传入的 HTTP 消息中,确实有两段信息可用来确定要在该类中调用哪种方法:SOAPAction 头或请求元素的名称(例如,soap:Body 元素中元素的名称)。在本例中,任何一个都指出了发送方想调用的方法的名称。
在默认情况下,.asmx 处理程序使用 SOAPAction 头的值来执行消息调度。因此,.asmx 处理程序查看消息中的 SOAPAction 头,使用 .NET 反射检查被引用类中的方法。它只考虑标记了 [WebMethod] 属性的方法,但是它可以通过查看每种方法的 SOAPAction 值来正确地确定要调用哪种方法。由于我们没有对类中的方法显式指定 SOAPAction 值,因此,.asmx 处理程序假设 SOAPAction 值将由 Web 服务的命名空间及其后面的方法名称组成。而且我们也没有指定命名空间,因此该处理程序会将 http://tempuri.org 作为默认的命名空间。这样,Add 方法的默认 SOAPAction 值将是 http://tempuri.org/Add。
您可以自定义 Web 服务的命名空间,方法是用 [SoapDocumentMethod] 属性批注自己的 WebMethod,从而使用 [WebService] 属性以及正确的 SOAPAction 值来批注自己的类,如下所示:
- using System.Web.Services;
 - using System.Web.Services.Protocols;
 - [WebService(Namespace="http://example.org/math")]
 - public class MathService
 - {
 - [WebMethod]
 - public double Add(double x, double y) {
 - return x + y;
 - }
 - [WebMethod]
 - [SoapDocumentMethod(Action="urn:math:subtract")]
 - public double Subtract(double x, double y) {
 - return x - y;
 - }
 - ...
 - }
 
现在,.asmx 处理程序希望 Add 方法的 SOAPAction 值是 http://example.org/math/Add(使用默认试探法),希望 Subtract 方法的 SOAPAction 值是 urn:math:subtract(因为我们将它显式定义为该值)。例如,下面的 HTTP 请求消息调用 Subtract 操作:
- POST /math/math.asmx HTTP/1.1
 - Host: localhost
 - Content-Type: text/xml; charset=utf-8
 - Content-Length: length
 - SOAPAction: "urn:math:subtract"
 - < soap:Envelope
 - xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
 - >
 - < soap:Body>
 - < Subtract xmlns="http://example.org/math">
 - < x>33< /x>
 - < y>66< /y>
 - < /Subtract>
 - < /soap:Body>
 - < /soap:Envelope>
 
如果 .asmx处理程序未找到与传入的 HTTP 消息相匹配的 SOAPAction,则它只是引发一个异常(以后会详细介绍如何处理异常)。如果您不想依赖 SOAPAction 头来进行方法调度,则可以用 [SoapDocumentService] 属性的 RoutingStyle 属性来批注该类,以便指示 .asmx 处理程序使用请求元素的名称。如果这样做的话,可能还应该指出 WebMethod 不需要 SOAPAction 值(通过将它们的值设置为空字符串),如下所示:
- using System.Web.Services;
 - using System.Web.Services.Protocols;
 - [WebService(Namespace="http://example.org/math")]
 - [SoapDocumentService(
 - RoutingStyle=SoapServiceRoutingStyle.RequestElement)]
 - public class MathService
 - {
 - [WebMethod]
 - [SoapDocumentMethod(Action="")]
 - public double Add(double x, double y) {
 - return x + y;
 - }
 - [WebMethod]
 - [SoapDocumentMethod(Action="")]
 - public double Subtract(double x, double y) {
 - return x - y;
 - }
 - ...
 - }
 
在本例中,该处理程序甚至不查看 SOAPAction 值,而是使用请求元素的名称。例如,对于 Add 方法,该处理程序希望请求元素的名称是 Add(来自 http://example.org/math 命名空间),如下面的 HTTP 请求消息中所示:
- POST /math/math.asmx HTTP/1.1
 - Host: localhost
 - Content-Type: text/xml; charset=utf-8
 - Content-Length: length
 - SOAPAction: ""
 - < soap:Envelope
 - xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
 - >
 - < soap:Body>
 - < Add xmlns="http://example.org/math">
 - < x>33< /x>
 - < y>66< /y>
 - < /Add>
 - < /soap:Body>
 - < /soap:Envelope>
 
因此,当 .asmx处理程序收到传入的 HTTP 消息时,它做的第一件重要事情就是确定如何将该消息调度到相应的 WebMethod。但是,在能够实际调用该方法之前,它必须将传入的 XML 映射到 .NET 对象。
Copyright © 2009-2022 www.wtcwzsj.com 青羊区广皓图文设计工作室(个体工商户) 版权所有 蜀ICP备19037934号