How ASP. NET web service works (3)

Source: Internet
Author: User
ArticleDirectory
    • Automatically generate the WSDL document
    • Summary

To enable the. asmx handle to deserialize the SOAP header, you must first define a. Net class, which represents the implicit XML Schema class. In this example, the corresponding classes are as follows:

[Xmltype (namespace = "http://example.org/security")]
[Xmlroot (namespace = "http://example.org/security")]
Public class userNameToken: soapheader {
Public String username;
Public String password;
}

Then, you need to define a member variable in the webmethod class to control the instance of a header class. Similarly, you must mark the [soapheader] attribute for webmethods. See below:

Using system;
Using system. Web. Services;
Using system. Web. Services. Protocols;
[WebService (namespace = "urn: geometry")]
Public class geometry {
Public userNameToken token;
 
[Webmethod]
[Soapheader ("token")]
Public double distance (point orig, point DEST ){
If (! Token. username. Equals (reverse (token. Password )))
Throw new exception ("Access Denied ");
Return math. SQRT (math. Pow (orig. x-dest.x, 2) +
Math. Pow (orig. y-dest.y, 2 ));
}
}

In this way, you can access the token domain in webmethod and extract the information provided by the SOAP header. You can also use the same technology to send the header information back to the client-you need to specify the header direction in the [soapheader] attribute declaration.

The. asmx handle also provides automatic serialization of. Net exceptions. Any unprocessed exceptions hijacked by the. asmx handle will be automatically serialized as the soap Fault Element in the Response Message. For example, in the previous example, if the user name does not match the reversed passwordCodeWill throw a. Net exception .. The asmx handle captures this exception and serializes it into the following SOAP response:

<Soap: Envelope
Xmlns: Soap = "http://schemas.xmlsoap.org/soap/envelope"
>
<Soap: Body>
<Soap: Fault>
<Faultcode> soap: server </faultcode>
<Faultstring> server was unable to process request. -- & gt; Access Denied </faultstring>
<Detail/>
</Soap: Fault>
</Soap: Body>
</Soap: envelope>

If you want to control more soap fault elements, you can specify details of all soap fault elements to explicitly throw a soap exception object. For example, faultcode, faulstring, faultactor, and detail elements.

Understanding how webmethods works requires understanding the fundamental serialization engine and its various options. The advantage of the serialization engine is that it hides the underlying xml api code in the Custom handle. However, although many developers think this is good, some people think this is also a defect because they want to handle the unprocessed soap messages in webmethod.

Automatically generate the WSDL document

After a webmethod is written and deployed, the customer needs to clearly understand what the SOAP message should look like when it is successfully communicated with it. The standard way to provide web service description is through WSDL (defined with embedded XSD ).. The asmx handle automatically generates user-friendly readable documents and WSDL definitions, which accurately reflects the webmethod interface. If some ing attributes are applied to your webmethods, they will all be reflected in the generated documents.

If you browse this. asmx file, you will see a human readable document page, as shown in figure 2. This document page is generated by defaultwsdlhelpgenerator. aspx (in C: "Windows" Microsoft. NET "Framework" v1.0.3705 "config. Open this file and you will see that it is a standard ASP. NET page that uses. Net reflection to generate that document. This feature keeps the document synchronized with the code. You can modify this file to customize your generated document.

You can also specify a different document file in the web. config file to bypass the file generator based on the virtual directory:

<Configuration>
<System. Web>
<WebServices>
<Wsdlhelpgenerator href = "mydocumentation. aspx"/>
</WebServices>
...

If the client sends a request to the. asmx end point and adds "? The. asmx handle will generate a WSDL definition instead of a human-readable document. The client can use the WSDL definition to generate a proxy class, which automatically knows how to communicate with Web Services.

To customize the WSDL generation process, you can writeSoapextensionreflectorClass and register the webmethods framework in your web. config file. Then, when the. asmx handle generates a WSDL definition, it will call your reflector class to give you the opportunity to customize the final definition provided to the user.

You can also use two technologies to bypass the WSDL generation process. First, you can provide a static WSDL document in your virtual directory for the customer to access, and then remove the document generator from the Web. config file (as shown below ).

<Configuration>
<System. Web>
<WebServices>
<Protocols>
 
                                                 
  
... 

another automation technology is to use the [webservicebinding] attribute to specify the location of the static WSDL document implemented by the webmethod class in the virtual directory, you must also use the [soapdocumentmethod] attribute to specify the name of the WSDL binding for each implemented webmethod. After this is done, the automatic WSDL generation process will import your static WSDL file to generate a new service description.

it is extremely difficult to manually write WSDL because there are not many WSDL editors. Therefore, automatic document/WSDL generation is a valuable part of the webmethod framework. Without it, many developers may feel sad.

conclusion

the ASP. NET webmethods framework provides an efficient method for creating web services. Webmethods enables traditional. Net methods to become web service operations that support HTTP, XML, XML schema, and WSDL. The webmethod (. asmx) handle automatically determines how to dispatch the incoming SOAP request message to an appropriate method, and then serializes the XML Element in the request message into the corresponding. Net object. To simplify the integration client, the. asmx handle also supports the generation of readable documents and WSDL.

compared with custom ihttphandlers, although the webmethods framework is somewhat limited, it also provides a powerful extension model called the soap extension framework. The soap extension allows you to introduce additional features we have not discussed above to meet your specific needs. For example, Microsoft released web serivices enhancements1.0, which provides the soapextension class and introduced several gxa specifications for the webmethods framework.

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.