Go How asp. net Web service works

Source: Internet
Author: User
Tags pow wsdl
<span id="Label3"> </p>Https://msdn.microsoft.com/zh-cn/library/ms996410.aspx Release Date: 4/1/2004 | Update on: 4/1/2004 summary: Learn how the Microsoft asp. NET Web services method (WEBMETHOD) provides an efficient way to build web Services. WebMethod can expose traditional Microsoft. NET methods to WEB service operations that support HTTP, xml, XML schemas, SOAP, and WSDL. The WebMethod (. asmx) handler automatically dispatches the incoming SOAP message to the appropriate method and automatically serializes the incoming XML element into the corresponding. NET Object. (20 printed pages) Aaron Skonnard DevelopMentor2003 May applies to: Microsoft? Asp. NET Web service method SOAP Message processing XML Schema HTTP-based WEB Service * Introduction to WEBMETHOD framework message scheduling XML mapping to Objects Auto-generating WSDL Summary resource Introduction at Microsoft?. NET, There are currently two distinct ways to implement HTTP-based Web Services. The first and lowest-level approach is to write a custom IHttpHandler class and insert it into The. NET HTTP pipeline. This approach requires that you use the System.Web API to process incoming HTTP messages and use the System.Xml API to handle the SOAP wrappers found in the HTTP Body. Writing a custom handler also requires you to manually write a WSDL document that accurately describes your Implementation. To do this correctly, you need to have an in-depth understanding of XML, XSD, SOAP, and WSDL specifications, but this precondition is daunting for most people. A more efficient way to implement WEB services is to use the Microsoft asp. WebMethod Framework. The Asp. asmx endpoint comes with a special IHttpHandler class (named Webservicehandler) that provides the reusable XML, XSD, SOAP, and WSDL functionality you need. Because the WebMethod framework frees you from complex underlying XML technologies, you can quickly focus on the business problem at Hand. Ms996410.howwebmeth_01(zh-cn,msdn.10). GIF Figure 1. The tradeoff between flexibility and productivity is a common tradeoff between the flexibility and productivity shown in 1 by choosing between implementation Technologies. Writing a custom IHttpHandler gives you great flexibility, but it takes a lot of time to write, test, and debug your Code. The WebMethod framework makes it easy to quickly build and run Web services, but you will undoubtedly be constrained by the boundaries of that Framework. however, if the WebMethod framework does not fully meet your needs, you can also extend the framework by adding your own additional Features. In general, unless you have mastered XML, XSD, SOAP, and WSDL and are willing to take the burden of dealing directly with them, It is best to use the WebMethod framework to meet your WEB service NEEDS. It provides the basic services required for most WEB service endpoints, and some interesting extensibility features that make the framework more consistent with your exact needs. Based on the above assumptions, the remainder of this article will discuss the internal working mechanism of WEBMETHOD. If you are unfamiliar with XML schemas and soap, you may need to refer to understanding XML Schema and understanding soap before continuing to read this Article. Returns to the top WebMethod Framework WebMethod framework is designed around the way a SOAP message is mapped to a method on A. NET class. This is done by first annotating your method with the [WebMethod] property found in the System.Web.Services \ Namespace. For example, the Following. NET class contains four methods, of which two methods are annotated with the [WebMethod] property: 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) {retu RN x * y; } public double Divide (double x, double y) {return x/y;}} To use this class in the WebMethod framework, you need to edit the classInto the assembly, and then 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, while Multiply and Divide cannot (because they are not marked as [WebMethod]). The ADD and Subtract are exposed as WEB service operations through The. asmx Endpoint. To do this, create a new text file named Math.asmx that contains the following simple declaration, and then place it in the same virtual directory that contains the assembly (note: this is placed in the virtual directory itself, not its bin subdirectory): <%@ WebService class= " MathService "%> notice above the declaration that the ASMX handler is going to find WebMethod in which class, the handler magically handles everything Else. For example, assuming that the name of the virtual directory is ' math ', which contains math.asmx and a bin subdirectory containing the assembly, browsing to http://localhost/math/math.asmx causes The. asmx handler to generate the document page shown in 2 (the following Detailed). There is a big change in how The. asmx handler works. the. asmx file typically contains only the WebService declaration, which references the Web service class by name (this is similar to the declaration shown above). therefore, in this case, the assembly must have been compiled and deployed to the bin directory of the virtual directory. For source code found in The. asmx file, The. asmx handler also provides real-time Compilation. For example, the following file (named mathjit.asmx) contains the WebService declaration and the 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) {retu RN x * y; } PUBlic double Divide (double x, double y) {return x/y;}} When this file is first accessed over HTTP, The. asmx handler compiles the source code and deploys the assembly to the correct Location. Note that the WebService declaration must also provide the language so that The. asmx handler can select the correct compiler at run Time. The obvious disadvantage of this approach is that compilation errors are found only after the first access to the File. ms996410.howwebmeth_02 (zh-cn,msdn.10). GIF Figure 2. MathService documents when you create a new WEB service project in Visual Studio. NET, you always use the "dual file" technique, which is that the source file for the class is separate from The. asmx file that references it. The integrated development environment (IDE) hides files as much as possible, but if you click Show All files on the solution Explorer toolbar, you will notice that each Web service class in the project has two Files. In fact, Visual Studio. NET does not support syntax highlighting or IntelliSense for. asmx files, So 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 into the bin directory of the virtual directory. Before discussing in detail how the ASMX handler works, let's briefly discuss how the message is passed from Internet information Server (IIS) to The. asmx handler. When an incoming HTTP message arrives at Port 80 o'clock, IIS uses the information found in the IIS metabase to determine which ISAPI DLL should be used to process the Message. the. NET installer maps the. asmx extension to aspnet_isapi.dll, as shown in 3. ms996410.howwebmeth_03 (zh-cn,msdn.10). gif Figure 3. The. asmx IIS application mapping Aspnet_isapi.dll is the standard ISAPI extension provided by The. NET framework, which simply forwards the HTTP request to a separate worker process named Aspnet_wp.exe. aspnet_wp.exe hosts the common language runtime and The. NET HTTP pipeline. When a message enters The. NET HTTP pipeline, The pipeline is in the configuration fileLook for the IHttpHandler class that should be used for a given Extension. If you are looking in the Machine.config file, you will find that it contains the HttpHandler mapping for The. asmx file, as Follows: <configuration> <system.web>
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.