How to work on ASP.NET Web Service (1)

zhaozj2021-02-16  65

How to work on ASP.NET Web Service (1)

Summary

How does the ASP.NET Web Service Method provide an efficient solution for creating a web service. WebMethods makes traditional Microsoft.Net methods a web service, which supports HTTP, XML, XML Schema, SOAP, and WSDL. The webmethods (.asmx) handle is sent to the appropriate method to the appropriate method and serialize the arrival XML element to the corresponding .NET object.

Introduction

Today, there are two ways to do two ways based on HTTP-based web services in Microsoft.NET. The first one is also a lower level of technology to write a custom IHTTPHANDLER class and embed it into the HTTP pipe. This approach requires you to use the http message from the System.Web API to process the SOAP package in the HTTP message body with the System.xml API. Writing a custom handle also requires you to manually write a WSDL document, accurate description of the implementation process. All of this requires an in-depth understanding of XML, XSD, SOAP, and WSDL specification, but this is a prerequisite for most people.

A more efficient way to implement Web services is to use the WebMethods framework for Microsoft ASP.NET. ASP.NET loads a dedicated IHTTPHANDLER class for. Sasmx financing nodes (called WebServiceHandler), which provides you with XML, XSD, SOAP, and WSDL functional models. Because the WebMethod framework allows you to free from the complexity of the underlying XML technology, you can concentrate on some important business issues.

Figure 1. Tradeoff Between Flexibility and Productivity

One implementation technique involves the trade-off between flexibility and efficacy, as shown in Figure 1. A custom IHTTPHANDLER has great flexibility, but it costs a lot of time to write, test and debug code. The WebMethods framework makes it easy to build and run web services, but it is obvious that you will be restricted to the boundary of the frame. However, if the WebMethods framework does not meet your needs, you can extend the framework by filling your own feature.

In general, unless you have already mastered XML, XSD, SOAP, and WSDL and is willing to withstand direct processing, it is best to use the WebMethods framework to implement your web service. It provides basic services for most web service endpoints, and some extension properties make the architecture more suitable for your specific needs. Based on this assumption, the remainder of the article we will discuss the internal working mechanism of WebMethods.

WebMethods framework

The WebMethods framework maps the SOAP message to a .NET class method by tagning the [WebMethods] property at the beginning of the method, [WebMethods] can be found in the System.Web.Service namespace. For example, the next .NET class includes four methods, two of which are labeled [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 a web service, while Multiply and Divide cannot. (Because they are not marked as [WebMethods])

You can access the Add and Subtract Web services through a .asmx financing: Create a text file Math.asmx, which contains the following simple declaration, then put it in the same virtual directory containing Assembly (Note is a virtual The catalog itself, not its bin subdirectory)

<% @ Webservice Class = "MathService"%>

This declaration tells the .asmx handle which class looks for WebMethods, and the remaining will be processed by the handle. For example, it is assumed that the virtual directory is called "Math", which contains Math.asmx, which contains assembly in its bin subdirectory, and browsing http: //localhost/math/math.asmx .asmx handle will generate Figure 2 Document page.

Figure 2. MathService Documentation

There is a major change on how the .asmx handle works. .asmx files typically only contains the WebService declaration, reference the corresponding web service class according to the name. Therefore, in this case, Assembly must have been compiled and deployed into the bin subdirectory of the virtual directory. The .asmx handle also provides an instant-in-time compiration of the .asmx file source code, such as the following files including the WebService declaration, including 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 by HTTP, the .asmx handle compiles the source code and deploy Assembly to the appropriate position. Note The WebService declaration must provide a language to make the .asmx handle can select the correct compiler at runtime. This method of obvious disadvantage is until the first visit to this file, you will find its compilation error.

When you create a new Web Service project in Visual Studio, you usually use the "Dual File" technology, that is, the class source file, and the referenced .asmx file is separated. IDE will try to shield these if you click Show All Files in the Solution Explorer toolbar, and you will notice that there are two files in the Web Service class in the project. In fact, Visual Studio.NET does not support. Syntax Highlighting or IntelliSense® for .asmx files. For web projects, Visual Studio.NET is also responsible for creating a virtual directory, automatically put compiling Assembly in the bin subdirectory of the virtual directory. Let's talk about how the .asmx handle is working, how is the message from IIS to .asmx handles. When an HTTP message reaches an 80-port, IIS determines which isapi.dll found in the IIS metadata database. The .asmx extension is mapped to ASPNET_ISAPI.dll during .NET installation, as shown in Figure 3.

Figure 3. IIS Application Mapping for .asmx

ASPNET_ISAPI.DLL is the standard ISAPI extension provided by the .NET framework, which is just simply transmitting an HTTP request to a separate worker process aspnet_wp.exe. ASPNET_WP.EXE HOSTS CLR (General Language Runtime) and HTTP Pipeline. Once the message enters the HTTP pipe, the pipelines finds which ihttphandler class to see which IHTTPHANDLER class is used to process a given extension. If you view your Machine.config file, you will see that it contains a httphandler that maps to .asmx files, as shown below:

When an access .asmx file enters the .NET HTTP pipe, the pipe will call the WebServiceHandlerFactory class to instantiate a new WebServiceHandler object, with it to handle the request (by calling the ihttphandlerprocessRequest method). The WebServiceHandler object opens the physical.asmx file to determine the class name that contains your webmedhods.

Note: Not finished,

The rest of the content is what works on ASP.NET Web Service (2)

How to work on ASP.NET Web Service (3)

转载请注明原文地址:https://www.9cbs.com/read-23383.html

New Post(0)