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:
- using System.Web.Services;
- public class MathService
- {
- [WebMethod]
- public double Add(double x, double y) {
- return x + y;
- }
- [WebMethod]
- public double Subtract(double x, double y) {
- return x - y;
- }
- public double Multiply(double x, double y) {
- return x * y;
- }
- public double Divide(double x, double y) {
- return x / y;
- }
- }
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 ):
- < %@ 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.
- < @% WebService class="MathServiceJit" language="C#"%>
- using System.Web.Services;
- public class MathServiceJit
- {
- [WebMethod]
- public double Add(double x, double y) {
- return x + y;
- }
- [WebMethod]
- public double Subtract(double x, double y) {
- return x - y;
- }
- public double Multiply(double x, double y) {
- return x * y;
- }
- public double Divide(double x, double y) {
- return x / y;
- }
- }
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:
- < configuration>
- < system.web>
- < httpHandlers>
- < add verb="*" path="*.asmx" type="System.Web.Services.Protocols.WebServiceHandlerFactory,
- System.Web.Services, Version=1.0.3300.0, Culture=neutral,
- PublicKeyToken=b03f5f7f11d50a3a" validate="false"/>
- ...
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.
- . 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
- Implementation of cookieless sessions