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

zhaozj2021-02-16  53

Once the HTTP pipe is called, XML, XSD, SOAP, and WSDL processing are started. The remaining function provided by the .asmx handle is divided into three areas:

Message assignment

When the .asmx handle is called by the HTTP pipe, determine which .NET class is checked by viewing the WebService declaration in the .asmx file. Then it observes the information in the HTTP message to determine which method in the call reference class. In order to call the ADD method in the previous example, the HTTP request message should be just like the following:

Post /math/math.asmx http / 1.1

Host: Localhost

Content-type: text / xml; charSet = UTF-8

Content-Length: Length

SOAPACTION: "http://tempuri.org/add"

XMLns: soap = "http://schemas.xmlsoap.org/soap/envelop/"

>

33

66

Two information on the above HTTP request message can be used to determine which method in the call class: the name of the request element in the SOAPAction header or SOAP body. In this example, each method pointed out the name of the sender wants to call.

The .asmx handle uses the value of the soapaction header to assume the assignment of the message. Therefore, .asmx handle view the SOAPAction header in the message, use the method in the reference class using the .NET mapping. It only considers how to mark the [WebMethod] attribute, but which method is determined by looking at the soapaction value of each method. Because we don't have a clear specified SOAPAction in the class, the .asmx handle thinks that the value of the soapaction is the namespace of the web service plus method name. And we don't specify a name space, so the handle takes http://tempuri.org as the default. The default soapaction value of this add method is http://tempuri.org/add.

The namespace of the Web service can be customized as follows. Tag WebMethods with the [SOAPDocumentMethod] property of the class tag to specify the specific soapaction value with the [SOAPDocumentMethod] property. Examples are as follows:

Using system.Web.services;

Using system.web.services.protocols;

[WebSpace (Namespace = "http://example.org/math")]]

Public Class MathService

{

[WebMethod]

Public Double Add (double x, double y) {

Return X Y;

}

[WebMethod]

[SOAPDocumentMethod (action = "URN: MATH: SUBTRACT")]

Public Double Subtract (double x, double y) {

Return X - Y;

}

...

}

Now .asmx handle thinks the SOAPACTION value of the add method is http://example.org/math/add, Subtract's value is URN: Math: Subtract (Because we are clearly defined in the class). For example, the following HTTP request message call Subtract: post /math/math.asmx http / 1.1host: localhost

Content-type: text / xml; charSet = UTF-8

Content-Length: Length

SOAPACTION: "URN: MATH: SUBTRACT"

XMLns: soap = "http://schemas.xmlsoap.org/soap/envelop/"

>

33

66

If the .asmx handle does not find a soapAction match for the HTTP request message, an exception will be thrown. If you don't want to deserve the SOAPAction header assignment message, you can boot the .asmx handle use the request element name. This method requires the ROUTINGSTYLE feature of [SOAPDocumentService] attribute on the class mark, and should also indicate that WebMethods do not need a soapaction value (set it in the class). As follows:

Using system.Web.services;

Using system.web.services.protocols;

[WebSpace (Namespace = "http://example.org/math")]]

[SOAPDocumentService "

RoutingStyle = soapserviceerventingstyle.requestelement)]

Public Class MathService

{

[WebMethod]

[SOAPDocumentMethod (action = ")]]]

Public Double Add (double x, double y) {

Return X Y;

}

[WebMethod]

[SOAPDocumentMethod (action = ")]]]

Public Double Subtract (double x, double y) {

Return X - Y;

}

...

}

In this case, the handle does not even care about the value of SOAPAction, which uses the name of the request element to determine the calling method. For example, in the HTTP request message below, it wants to call the name of the request element of the Add method is Add:

Post /math/math.asmx http / 1.1

Host: Localhost

Content-type: text / xml; charSet = UTF-8

Content-Length: Length

SOAPACTION: ""

XMLns: soap = "http://schemas.xmlsoap.org/soap/envelop/"

>

33 66

So when .asmx handle receives the HTTP message, the first thing it wants is to determine how to assign messages to the corresponding webmethod. Before it really calls the method, you need to map the coming XML to the .NET object.

Map XML to the object

Once the WebMethod handle determines which method calls, it will definite the XML message to the .NET object. As the message is assigned, the handle checks the class via the REFLECTION, and then determines how to process the XML message. The XMLSerializer class automatically completes mapping in XML and System.xml.Serialization Namespaces.

XMLSerializer can implement any .NET public type to XML Schema type mapping, with this appropriate mapping, which automatically implements mapping .NET objects and XML instance documents (see Figure 4). XMLSerializer is restricted by XML Schema, although all complex modern object models (such as non-tree-type object maps) can not be handled, but can handle complicated types of developers.

Look at the example of the previous add, XMLSerializer will map the X and Y elements to the .NET's Double value (must be provided when calling the Add method). The Add method returns a Double type value to the caller, which also needs to be serially connected to an XML element in the SOAP response message.

Figure 4. Mapping XML to Objects

XMLSerializer can also automatically process some complex types (except for some of the above restrictions). For example, the following WebMethod calculates the distance between the two points.

Using system;

Using system.Web.services;

Public class point {

Public Double X;

Public Double Y;

}

[WebService (Namespace = "URN: Geometry)]]

Public class geometry {

[WebMethod]

Public Double Distance (Point ORIG, POINT DEST) {

Return Math.SQRT (Math.Pow (Orig.x-Dest.x, 2)

Math.pow (Orig.y-dest.y, 2));

}

}

The SOAP message that requests this will contain a distance element that contains two child elements, one is called Orig, and the other is DEST, each includes x and y elements, as shown below:

XMLns: soap = "http://schemas.xmlsoap.org/soap/envelop/"

>

0

0

3

4

In this case, the SOAP response message will contain a distanceResponse element that contains a DOUBLE type DistanceResult child element.

XMLns: soap = "http://schemas.xmlsoap.org/soap/envelop/"

>

XMLns = "URN: Geometry">

5

The default XML mapping use method name as the request element name, the parameter name as the child element name. The structure of each parameter depends on the structure of the type. The name of the public field and attribute is simple mapped to sub-elements, such as X and Y in the Point class. The name of the response element defaults to "Response" after the name of the request element, and the response element also contains a child element and is attached to the request element name. "Result". It is also possible to use some fixed mapping properties to break the standard XML mapping. For example, you can use the [XMLTYPE] property to customize the type of name and namespace, use the [XMlelelement] and [XMlattribute] property to control how to map parameters or class members into elements or properties, or use [SOAPDocumentMethod] attribute control How to map the method itself into the element name in the request / response message. For example, check the DISTANCE that is redefined below.

Using system;

Using system.Web.services;

Using system.web.services.protocols;

Using system.xml.serialization;

Public class point {

[Xmlattribute]

Public Double X;

[Xmlattribute]

Public Double Y;

}

[WebService (Namespace = "URN: Geometry)]]

Public class geometry {

[WebMethod]

[SOAPDocumentMethod (RequestElementName = "Calcdistance",

ResponseElementName = "CalculatedDistance")]

[RETURN: XMLELEMENT ("Result")]

Public Double Distance

[XMLELEMENT ("O")] Point ORIG, [XMLELEMENT ("D")] Point Dest) {

Return Math.SQRT (Math.Pow (Orig.x-Dest.x, 2)

Math.pow (Orig.y-dest.y, 2));

}

}

The SOAP request message it expects is as follows:

XMLns: soap = "http://schemas.xmlsoap.org/soap/envelop/"

>

And will produce the following response message:

XMLns: soap = "http://schemas.xmlsoap.org/soap/envelop/">

5

The .asmx handle uses the SOAP Document / Literal style to implement and describe the default mappings displayed above. It means that the WSDL definition will contain the Literal XML Schema definition, which describes the request and response elements used in the SOAP message.

.ASMX handle can also use the SOAP RPC / Encoded style. This means that the SOAP body contains an XML representative of the RPC call, and the parameter is serially connected to the SOAP coding rule. Implementing these only will be replaced with [SOAPDocumentService] and [SOAPDocumentMethod] with the [SOAPRPCService] attribute.

As you can see, we may completely customize a mapping from a given method to SOAP messages. XMLSerializer provides a powerful serialization engine.

In addition to the anti-serialization of the parameters, .asmx handle can also serialize / deactivate the SOAP header. The processing of the SOAP head is different from the parameters because they are considered to be a typical uncontrollable information, and there is no direct contact with the specific method. Because of this, the header is mainly through the interception dayers, which is completely shielded for WebMethods.

However, if you want to get involved in the header information in WebMethod, you must provide a .NET class, derive from SoapHeader, which represents the XML Schema type of the head. This type of member variable is then defined as a placeholder of each header instance. Finally marks the WebMethod to which you want to visit, specify the domain name you want to reach.

For example, the SOAP request below includes a UserNameToken header for authentication.

XMLns: soap = "http://schemas.xmlsoap.org/soap/envelop/"

>

Mary

YRAM

...

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

New Post(0)