ArticleDirectory
- Summary
- Introduction
- Webmethods framework
Summary
How does ASP. NET Web Service (webmethods) provide an efficient solution for creating web services. Webmethods makes traditional Microsoft. NET Methods web service operations. It supports HTTP, XML, XML schema, soap, and WSDL. The webmethods (. asmx) handle sends the incoming SOAP message to an appropriate method and serializes the incoming XML element into a corresponding. Net object.
Introduction
There are two fundamentally different methods to implement HTTP-based Web Services in Microsoft. NET today. The first and lower-level technology is to compile a custom ihttphandler class and embed it into the 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 (envelope) in the HTTP message body ). Writing a custom handle also requires you to manually write the WSDL document to accurately describe the implementation process. To achieve this all requires an in-depth understanding of the XML, XSD, soap, and WSDL specifications, but this is a daunting prerequisite for most people.
A more efficient way to implement web services is to use the Microsoft ASP. NET webmethods framework. ASP. NET loads a specialized ihttphandler class for the. asmx End Node (webservicehandler). It provides functional templates for XML, XSD, soap, and WSDL for your needs. Because the webmethod framework frees you from the complexity of the underlying XML technology, you can focus on some important business problems.
Figure 1. tradeoff between flexibility and productivity
Selecting an implementation technique involves a trade-off between flexibility and efficiency, as shown in figure 1. A custom ihttphandler has great flexibility, but it takes a lot of time to write, test, and debug it.Code. The webmethods framework makes it very easy to build and run web services, but it is obvious that you will be limited to the boundaries of the framework. However, if the webmethods framework does not meet your needs correctly, you can add your own functions to extend the framework.
In general, unless you have mastered XML, XSD, soap, and WSDL and are willing to handle them directly, it is best to use the webmethods framework to implement your web service needs. It provides basic services for most web service endpoints, and some extension attributes make the architecture more suitable for your specific needs. Based on this assumption, the rest of the article will discuss the internal working mechanism of webmethods.
Webmethods framework
The webmethods framework maps a SOAP message to A. Net method by marking the [webmethods] attribute at the beginning of the method. [webmethods] can be found in the system. Web. Service namespace. For example, the following. Net class contains four methods, two of which are labeled with the [webmethods] 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 webmethods framework, you need to compile it into an assembly and copy it to the bin subdirectory of the virtual directory. In this example, the add and subtract methods are operated as Web Services, but multiply and divide cannot. (Because they are not marked as [webmethods])
You can use. asmx end node to access add and subtract web service operations: Create a text file math. asmx, which contains the following simple declaration, and then put it under the same virtual directory containing assembly (note that this is the virtual directory itself, rather than its bin subdirectory)
<% @ WebService class = "mathservice" %>
This statement tells the. asmx handle to find webmethods in which class, and the rest will be fully processed by the handle. For example, assume that the virtual directory is called Math, which contains math. asmx. Its bin sub-directory contains assembly and browses http: // localhost/Math/math. asmx. the asmx handle will generate the document page in Figure 2.
Figure 2. mathservice documentation
There is a major change in how the. asmx handle works .. The asmx file usually contains only the WebService declaration, and the corresponding web service class is referenced according to the name. Therefore, in this case, the Assembly must have been compiled and deployed to the bin subdirectory of the virtual directory .. The asmx handle also providesSource codeJust-in-time compilation. For example, the following file includes both the WebService Declaration and the source code of the reference 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 this file is accessed for the first time through HTTP, The. asmx handle compiles the source code and deploys the assembly to the corresponding location. Note that the WebService declaration must provide a language so that the. asmx handle can select the Correct Compiler at runtime. The obvious disadvantage of this method is that you will not find its compilation error until you first access this file.
When you create a new Web Service Project in Visual Studio. NET, the "double file" Technology is usually used, that is, the class source file is separated from the. asmx file that references it. IDE will try to block this. If you click show all files in the Solution Explorer toolbar, you will notice that the Web service class in the project has two files. In fact, Visual Studio. NET does not support syntax highlighting or intelliisense of. asmx files. For web projects, Visual Studio. NET is also responsible for creating a virtual directory and automatically placing compiled assembly under the bin subdirectory of the virtual directory.
Before we discuss in detail how the. asmx handle works, let's take a simple look at how messages are transmitted from IIS to. asmx handles. When an HTTP message reaches port 80, the information that IIS finds in the IIS metadatabase determines which ISAPI. dll is used to process the message .. . Asmx extension is mapped to aspnet_isapi.dll during installation, as shown in 3.
Figure 3. IIS application mapping for. asmx
. Aspnet_wp.exe hosts CLR (runtime in general language) and HTTP pipeline. Once a message enters the HTTP pipeline, the pipeline looks for the configuration file to see which ihttphandler class is used to process the given extension. If you view your machine. config file, you will see that it contains an httphandler mapped to the. asmx file, as shown below:
when an access. the message of the asmx file enters. net HTTP pipeline, the pipeline will call the webservicehandlerfactory class to instantiate a new webservicehandler object and use it to process requests (by calling Use the ihttphandlerprocessrequest method ). The webservicehandler object opens the physical. asmx file to determine the class name that contains your webmethods.
Note: incomplete,
For more information, see ASP. NET Web Service (2)
Asp. NET Web Service (3)