.asmx处理程序提供的功能之消息调度

开发 后端
本文介绍了.asmx处理程序提供的功能之消息调度。在默认情况下,.asmx 处理程序使用 SOAPAction 头的值来执行消息调度。

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

  1. POST /math/math.asmx HTTP/1.1   
  2. Host: localhost   
  3. Content-Type: text/xml; charset=utf-8   
  4. Content-Length: length   
  5. SOAPAction: "http://tempuri.org/Add"   
  6. < soap:Envelope    
  7. xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"   
  8. >   
  9. < soap:Body>   
  10. < Add xmlns="http://tempuri.org/">   
  11. < x>33< /x>   
  12. < y>66< /y>   
  13. < /Add>   
  14. < /soap:Body>   
  15. < /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 值来批注自己的类,如下所示:

  1. using System.Web.Services;   
  2. using System.Web.Services.Protocols;   
  3. [WebService(Namespace="http://example.org/math")]   
  4. public class MathService   
  5. {   
  6. [WebMethod]   
  7. public double Add(double x, double y) {   
  8. return x + y;   
  9. }   
  10. [WebMethod]   
  11. [SoapDocumentMethod(Action="urn:math:subtract")]   
  12. public double Subtract(double x, double y) {   
  13. return x - y;   
  14. }   
  15. ...   
  16. }   

现在,.asmx 处理程序希望 Add 方法的 SOAPAction 值是 http://example.org/math/Add(使用默认试探法),希望 Subtract 方法的 SOAPAction 值是 urn:math:subtract(因为我们将它显式定义为该值)。例如,下面的 HTTP 请求消息调用 Subtract 操作:

  1. POST /math/math.asmx HTTP/1.1   
  2. Host: localhost   
  3. Content-Type: text/xml; charset=utf-8   
  4. Content-Length: length   
  5. SOAPAction: "urn:math:subtract"   
  6. < soap:Envelope    
  7. xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"   
  8. >   
  9. < soap:Body>   
  10. < Subtract xmlns="http://example.org/math">   
  11. < x>33< /x>   
  12. < y>66< /y>   
  13. < /Subtract>   
  14. < /soap:Body>   
  15. < /soap:Envelope>   

如果 .asmx处理程序未找到与传入的 HTTP 消息相匹配的 SOAPAction,则它只是引发一个异常(以后会详细介绍如何处理异常)。如果您不想依赖 SOAPAction 头来进行方法调度,则可以用 [SoapDocumentService] 属性的 RoutingStyle 属性来批注该类,以便指示 .asmx 处理程序使用请求元素的名称。如果这样做的话,可能还应该指出 WebMethod 不需要 SOAPAction 值(通过将它们的值设置为空字符串),如下所示:

  1. using System.Web.Services;   
  2. using System.Web.Services.Protocols;   
  3. [WebService(Namespace="http://example.org/math")]   
  4. [SoapDocumentService(   
  5. RoutingStyle=SoapServiceRoutingStyle.RequestElement)]   
  6. public class MathService   
  7. {   
  8. [WebMethod]   
  9. [SoapDocumentMethod(Action="")]   
  10. public double Add(double x, double y) {   
  11. return x + y;   
  12. }   
  13. [WebMethod]   
  14. [SoapDocumentMethod(Action="")]   
  15. public double Subtract(double x, double y) {   
  16. return x - y;   
  17. }   
  18. ...   
  19. }   

在本例中,该处理程序甚至不查看 SOAPAction 值,而是使用请求元素的名称。例如,对于 Add 方法,该处理程序希望请求元素的名称是 Add(来自 http://example.org/math 命名空间),如下面的 HTTP 请求消息中所示:

  1. POST /math/math.asmx HTTP/1.1   
  2. Host: localhost   
  3. Content-Type: text/xml; charset=utf-8   
  4. Content-Length: length   
  5. SOAPAction: ""   
  6. < soap:Envelope    
  7. xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"   
  8. >   
  9. < soap:Body>   
  10. < Add xmlns="http://example.org/math">   
  11. < x>33< /x>   
  12. < y>66< /y>   
  13. < /Add>   
  14. < /soap:Body>   
  15. < /soap:Envelope>   

因此,当 .asmx处理程序收到传入的 HTTP 消息时,它做的第一件重要事情就是确定如何将该消息调度到相应的 WebMethod。但是,在能够实际调用该方法之前,它必须将传入的 XML 映射到 .NET 对象。

【编辑推荐】

  1. WebMethod框架:实现Web服务的更高效方法
  2. .NET框架基本要求(.NET1.1)
  3. P2PMessageQueue的实际用法
  4. 点对点消息队列函数:用于WinCE的IPC机制
  5. ASP.NET中无Cookie会话的优点与缺点
责任编辑:yangsai 来源: MSDN
相关推荐

2009-08-06 18:23:51

XML映射XmlSerializ.asmx处理程序

2009-08-07 14:14:27

自动生成WSDL.asmx处理程序

2009-06-14 17:18:55

ibmdwWebSphereMQ

2013-11-12 23:32:53

微信公号微信公众账号

2011-06-23 15:32:05

Qt Windows消息

2009-06-19 15:20:08

Quartz任务调度Spring

2009-08-19 15:54:33

处理C#消息

2009-06-17 16:56:46

Spring JMS

2013-08-20 16:14:46

pythonpython文本处理

2011-03-07 11:12:36

FileZilla

2022-07-01 17:14:03

消息通知鸿蒙

2020-02-18 13:05:44

Windows 10功能Windows

2014-05-22 14:57:28

Android消息处理机制Looper

2014-05-22 15:15:53

Android消息处理机制Looper

2014-05-22 15:18:25

Android消息处理机制Looper

2014-05-22 15:33:31

Android消息处理机制Looper

2014-05-22 15:45:58

Android消息处理机制Looper

2014-05-22 15:07:44

Android消息处理机制Looper

2014-05-22 15:48:50

Android消息处理机制Looper

2014-05-22 15:00:16

Android消息处理机制Looper
点赞
收藏

51CTO技术栈公众号