WebMethod framework: a more efficient way to implement Web Services

Source: Internet
Author: User

In Microsoft. NET, there are currently two completely different methods to implement HTTP-based Web Services. The first and lowest level method is to write a custom IHttpHandler class and insert it into the. net http pipeline. This method requires you to use the System. Web API to process incoming HTTP messages, and use the System. Xml API to process the SOAP encapsulation found in the HTTP body. Writing a custom processing program requires you to manually Write a wsdl document that accurately describes your implementation. To do this well correctly, you need to have a deep understanding of the XML, XSD, SOAP, and WSDL specifications, but this prerequisite is daunting for most people.

A more efficient way to implement Web services is to use the Microsoft ASP. NET WebMethod framework. ASP. NET provides a special IHttpHandler class named WebServiceHandler for the. asmx endpoint. This class provides the XML, XSD, SOAP, and WSDL functions that you need to apply repeatedly. Because the WebMethod framework frees you from the complex basic XML technology, you can focus on your business problems quickly.

Figure 1. compromise between flexibility and productivity
Selecting between implementation technologies forms a common compromise between flexibility and productivity as shown in 1. Writing custom IHttpHandler provides you with great flexibility, but it takes a lot of time to write, test, and debug code. The WebMethod framework makes it easy to quickly generate and run Web Services, but you are undoubtedly limited by the boundaries of the framework. However, if the WebMethod framework does not fully meet your needs, you can also expand the framework by adding your own additional features.

WebMethod framework
The WebMethod framework is designed to map SOAP messages to. NET classes. This is done by first using the [WebMethod] attribute found in the System. Web. Services \ namespace to annotate your method. For example, the following. NET class contains four methods, two of which are annotated using the [WebMethod] attribute:

 
 
  1. using System.Web.Services;   
  2. public class MathService   
  3. {   
  4. [WebMethod]   
  5. public double Add(double x, double y) {   
  6. return x + y;   
  7. }   
  8. [WebMethod]   
  9. public double Subtract(double x, double y) {   
  10. return x - y;   
  11. }   
  12. public double Multiply(double x, double y) {   
  13. return x * y;   
  14. }   
  15. public double Divide(double x, double y) {   
  16. return x / y;   
  17. }   
  18. }   

To use this class in the WebMethod framework, you need to compile the class into an assembly and copy it to the bin directory of the virtual directory. In this example, the Add and Subtract methods can then be exposed as Web service operations, but Multiply and Divide cannot be marked as [WebMethod] because they are not.

Use the. asmx endpoint to expose Add and Subtract as Web service operations. To this end, you need to create a new name named Math. asmx is a text file that contains the following simple statements, and then put it in the same virtual directory that contains the Assembly. Note: This is in the virtual directory itself, rather than its bin subdirectory ):

 
 
  1. < %@ WebService class="MathService"%> 

The above declaration notifies the. asmx handler to find the WebMethod in which class, And the handler will handle all other things magically. For example, assume that the virtual directory is named 'Math' and contains math. asmx and a bin subdirectory containing the Assembly browse http: // localhost/math. asmx will cause. the document page shown in asmx handler generation 2 will be detailed later ).

There is a big change in how the. asmx handler works .. The asmx file usually only contains the WebService declaration, which references the Web Service Class Based on the name, which is similar to the declaration shown above ). Therefore, in this example, the Assembly must have been compiled and deployed to the bin directory of the virtual directory. For the source code found in the. asmx file, the. asmx handler also provides real-time compilation. For example, the following file name is Mathjit. asmx) contains the WebService declaration and source code of the referenced class.

 
 
  1. < @% WebService class="MathServiceJit" language="C#"%>   
  2. using System.Web.Services;   
  3. public class MathServiceJit   
  4. {   
  5. [WebMethod]   
  6. public double Add(double x, double y) {   
  7. return x + y;   
  8. }   
  9. [WebMethod]   
  10. public double Subtract(double x, double y) {   
  11. return x - y;   
  12. }   
  13. public double Multiply(double x, double y) {   
  14. return x * y;   
  15. }   
  16. public double Divide(double x, double y) {   
  17. return x / y;   
  18. }   
  19. }   

When you access this file through HTTP for the first time, the. asmx handler will compile the source code and deploy the assembly to the correct location. Note that the WebService declaration must also provide a language so that the. asmx handler can select the Correct Compiler at runtime. The obvious disadvantage of this method is that the compilation error is discovered only after the file is accessed for the first time.

Figure 2. MathService document
When you are in Visual Studio? When creating a new Web Service Project in. NET, the "double file" Technology is always used, that is, the source file of the class is separated from the. asmx file that references it. The integrated development environment (IDE) tries its best to hide Files. However, if you click Show All Files on the Solution Explorer toolbar, you will notice that each Web service class in this project has two files. In fact, Visual Studio. NET does not support syntax highlighting of. asmx files or intelliisense ?, Therefore, if you are designing in this direction, you must rely on yourself. For Web projects, Visual Studio. NET is also responsible for automatically creating a virtual directory and compiling the assembly to the bin directory of the virtual directory.

Before discussing in detail how the. asmx processing program works, let's briefly discuss how messages are transmitted from Internet Information Server (IIS) to. asmx processing program. When the incoming HTTP message reaches port 80, IIS uses the information found in the IIS metadatabase to determine which isapi dll should be used to process the message .. . NET installer maps the. asmx extension to Aspnet_isapi.dll, as shown in 3.

Figure 3. IIS application ing of asmx
Aspnet_isapi.dll is a standard ISAPI extension provided by the. NET Framework. It only forwards HTTP requests to a separate auxiliary process named Aspnet_wp.exe. Aspnet_wp.exe host Common Language Runtime Library and. net http pipeline. After a message enters the. net http pipeline, the pipeline searches the configuration file to see which IHttpHandler class should be used for the given extension. If you search in the Machine. config file, you will find that it contains the httpHandler ing of the. asmx file, as shown below:

 
 
  1. < configuration>   
  2. < system.web>   
  3. < httpHandlers>     
  4. < add verb="*" path="*.asmx" type="System.Web.Services.Protocols.WebServiceHandlerFactory,    
  5. System.Web.Services, Version=1.0.3300.0, Culture=neutral,    
  6. PublicKeyToken=b03f5f7f11d50a3a" validate="false"/>   
  7. ...   

Therefore, when the message is oriented. asmx file. net http pipeline, the pipeline will call the WebServiceHandlerFactory class, so that a new WebServiceHandler object can be used to process requests by calling the IHttpHandlerProcessRequest method ). The WebServiceHandler object then opens the physical. asmx file to determine the name of the class containing WebMethod. For more information about how the. net http pipeline works, see HTTP Pipelines: Securely Implement Request Processing, Filtering, and Content Redirection with HTTP Pipelines in ASP. NET.

When the. asmx processing program is called by the. net http pipeline, it will magically process XML, XSD, SOAP, and WSDL.

  1. . NET Framework basic requirements (. NET1.1)
  2. P2PMessageQueue usage
  3. Point-to-Point Message Queue function: Used for the IPC Mechanism of WinCE
  4. Advantages and disadvantages of cookieless session in ASP. NET
  5. Implementation of cookieless sessions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.