After the. asmx handler is called by the HTTP pipeline, it determines which. NET class to check by viewing the WebService declaration found in the. asmx file. Then, it will view the information in the incoming HTTP message to correctly determine which method to call in the referenced class. To call the Add operation shown in the previous example, the imported HTTP message must have the following appearance:
- 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>
In the incoming HTTP message, there are indeed two pieces of information that can be used to determine which method to call in this class: the name of the SOAPAction header or request element, for example, soap: the name of the element in the Body element ). In this example, any one specifies the name of the method that the sender wants to call.
By default, the. asmx handler uses the value of the SOAPAction header to execute message scheduling. Therefore, the. asmx handler can view the SOAPAction header in the message and use. NET reflection to check the methods in the referenced class. It only considers the method marked with the [WebMethod] attribute, but it can correctly determine the method to be called by checking the SOAPAction value of each method. Because we didn't explicitly specify the SOAPAction value for the methods in the class, the. asmx handler assumes that the SOAPAction value will be composed of the Web service namespace and the method name following it. And we didn't specify the namespace, so the handler uses the http://tempuri.org as the default namespace. In this way, the default SOAPAction value of the Add method will be http://tempuri.org/add.
You can customize the Web Service namespace by using the [SoapDocumentMethod] attribute to annotate your WebMethod, so that you can use the [WebService] attribute and the correct SOAPAction value to annotate your class, as follows:
- 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;
- }
- ...
- }
Now, the. asmx handler wants the SOAPAction value of the Add method to be http://example.org/math/adduse the explain method), and the SOAPAction value of the Subtract method is urn: math: subtract because we explicitly define it as this value ). For example, the following HTTP Request Message calls the Subtract operation:
- 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>
If the. asmx handler does not find the SOAPAction that matches the incoming HTTP message, it only raises an exception and details how to handle the exception ). If you do not want to rely on the SOAPAction header for method scheduling, you can use the RoutingStyle attribute of the [SoapDocumentService] attribute to annotate the class to instruct the. asmx handler to use the name of the Request element. If this is done, it may also be pointed out that WebMethod does not require SOAPAction values by setting their values to null strings), as shown below:
- 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;
- }
- ...
- }
In this example, the handler uses the name of the Request element instead of viewing the SOAPAction value. For example, for the Add method, the handler wants the name of the Request element to be Add from the http://example.org/math namespace), as shown in the following HTTP Request Message:
- 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>
Therefore, when the. asmx processing program receives the incoming HTTP message, the first important thing it does is to determine how to schedule the message to the corresponding WebMethod. However, before you can actually call this method, it must map the passed XML to the. NET object.
- WebMethod framework: a more efficient way to implement Web Services
- . NET Framework basic requirements (. NET1.1)
- P2PMessageQueue usage
- Point-to-Point Message Queue function: Used for the IPC Mechanism of WinCE
- Advantages and disadvantages of cookieless session in ASP. NET