Web service implementation principle and asynchronous call

Source: Internet
Author: User
Tags oracleconnection
In the net2.0 environment, each method in the Web service generates an Asynchronous Method and an end event in the proxy class generated after adding a reference to the client. We can use this Asynchronous Method and event to easily call web service in different steps.

Simple Example

Assume that a data query method is defined in Web Service:

  1. [Webmethod]
  2. Public dataset executequery (string SQL)
  3. {
  4. Using (oracleconnection conn = new oracleconnection (connstrsql ))
  5. {
  6. Try
  7. {
  8. Conn. open ();
  9. Oraclecommand cmd = new oraclecommand (SQL, Conn );
  10. Oracledataadapter Ada = new oracledataadapter (CMD );
  11. Dataset DS = new dataset ();
  12. Ada. Fill (DS );
  13. Return Ds;
  14. }
  15. Catch (oracleexception ex)
  16. {
  17. Throw new exception (ex. Message );
  18. }
  19. Finally
  20. {
  21. Conn. Close ();
  22. }
  23. }

Client:

  1. /// Description:
  2. /// A graph with the words Dynamically Loaded in Panel1
  3. ///
  4. Public partial class frmstock: Form
  5. {
  6. Service. myservice W = new service. myservice (); // call the Web Service
  7. Public frmstock ()
  8. {
  9. Initializecomponent ();
  10. }
  11. Private void frmstock_load (Object sender, eventargs E)
  12. {
  13. // Add an Asynchronous Method to trigger an event after execution
  14. W. executequerycompleted + = new WMS. executequerycompletedeventhandler (w_executequerycompleted );
  15. }
  16. Private void btnfind_click (Object sender, eventargs E)
  17. {
  18. String SQL = "select * from test ";
  19. W. executequeryasync (SQL); // call the corresponding Asynchronous Method
  20. Changestatus (true );
  21. }
  22. Void w_executequerycompleted (Object sender, WMS. executequerycompletedeventargs E)
  23. {

In the net2.0 environment, each method in the Web service generates an Asynchronous Method and an end event in the proxy class generated after adding a reference to the client. We can use this Asynchronous Method and event to easily call web service in different steps.

Simple Example

Assume that a data query method is defined in Web Service:

  1. [Webmethod]
  2. Public dataset executequery (string SQL)
  3. {
  4. Using (oracleconnection conn = new oracleconnection (connstrsql ))
  5. {
  6. Try
  7. {
  8. Conn. open ();
  9. Oraclecommand cmd = new oraclecommand (SQL, Conn );
  10. Oracledataadapter Ada = new oracledataadapter (CMD );
  11. Dataset DS = new dataset ();
  12. Ada. Fill (DS );
  13. Return Ds;
  14. }
  15. Catch (oracleexception ex)
  16. {
  17. Throw new exception (ex. Message );
  18. }
  19. Finally
  20. {
  21. Conn. Close ();
  22. }
  23. }

Client:

  1. /// Description:
  2. /// A graph with the words Dynamically Loaded in Panel1
  3. ///
  4. Public partial class frmstock: Form
  5. {
  6. Service. myservice W = new service. myservice (); // call the Web Service
  7. Public frmstock ()
  8. {
  9. Initializecomponent ();
  10. }
  11. Private void frmstock_load (Object sender, eventargs E)
  12. {
  13. // Add an Asynchronous Method to trigger an event after execution
  14. W. executequerycompleted + = new WMS. executequerycompletedeventhandler (w_executequerycompleted );
  15. }
  16. Private void btnfind_click (Object sender, eventargs E)
  17. {
  18. String SQL = "select * from test ";
  19. W. executequeryasync (SQL); // call the corresponding Asynchronous Method
  20. Changestatus (true );
  21. }
  22. Void w_executequerycompleted (Object sender, WMS. executequerycompletedeventargs E)
  23. {
  24. Datatable dt = E. Result. Tables [0]; // obtain execution result E
  25. Changestatus (False);
  26. If(Dt. Rows. Count <= 0)
  27. {
  28. MessageBox. Show ("No data","Information", Messageboxbuttons. OK, messageboxicon. information );
  29. Return;
  30. }
  31. Gvshow. autogeneratecolumns =False;
  32. Gvshow. datasource = DT;
  33. }
  34. Private VoidBtnexit_click (ObjectSender, eventargs E)
  35. {
  36. This. Close ();
  37. }
  38. Private VoidChangestatus (BoolSign)
  39. {
  40. Panel1.visible = sign;
  41. }
  42. }
  43. In this way, you can

    1. Avoiding false positives

    2. Provide a friendly user experience

    Implementation Principle

    Web Service
    After the release, the client adds a reference, and then vs actually generates a local proxy class in the background at this time. Then it looks like
    Service operations are actually operations on the local proxy class. The agent class processes the network access logic, and the client is as simple and convenient as operating the local class.

    Client sends web
    After a service request arrives at the proxy class, the proxy class processes the request to obtain the soap data packet from the server, and then processes the data and forwards it to the client. About
    SOAP, WSDL, and other definitions can be simply understood as: Soap defines the format and rules for data transmission, while WSDL defines the Web
    Service messages and related operations, data transmission through HTTP transmission protocol...

    How do I define the proxy class?

    We can analyze the detailed information of the proxy class by customizing the proxy class.

    1. First create Web Service: Service. asmx

    2. Create and publish a virtual directory in IIS

    3.generate proxy classes through wsdl.exe.

    Open the SDK Command Prompt window, the following job:

    D: \ Program Files \ Microsoft Visual Studio
    8 \ SDK \ V2.0> WSDL http: // localhost/serve/service. asmx? WSDL
    Microsoft
    (R) Web Services Description Language Utility
    [Microsoft (R). Net
    Framework, version 2.0.50727.42]
    Copyright (c) Microsoft Corporation.
    All rights reserved.
    Writing File 'd: \ Program Files \ Microsoft Visual
    Studio 8 \ SDK \ V2.0 \ service. CS '.

    Here, Service. CS is the generated proxy class, which is visible to open, involving Asynchronous methods and event generation. (PartCode
    As shown below)

     


    1.  /// <Remarks/>
    2. Public EventHelloworldcompletedeventhandler helloworldcompleted;
    3. /// <Remarks/>
    4. Public EventShowinfocompletedeventhandler showinfocompleted;
    5. /// <Remarks/>
    6. [System. Web. Services. Protocols. soapdocumentmethodattribute (Http://tempuri.org/HelloWorld", Requestnamespace ="Http://tempuri.org /", Responsenamespace ="Http://tempuri.org /", Use = system. Web. Services. description. soapbindinguse. literal, parameterstyle = system. Web. Services. Protocols. soapparameterstyle. Wrapped)]
    7. Public StringHelloworld (){
    8. Object[] Results =This. Invoke ("Helloworld",New Object[0]);
    9. Return((String) (Results [0]);
    10. }
    11. /// <Remarks/>
    12. PublicSystem. iasyncresult beginhelloworld (system. asynccallback callback,ObjectAsyncstate ){
    13. Return This. Begininvoke ("Helloworld",New Object[0], callback, asyncstate );
    14. }
    15. /// <Remarks/>
    16. PublicStringEndhelloworld (system. iasyncresult asyncresult)
    17. {
    18. Object[] Results =This. Endinvoke (asyncresult );
    19. Return((String) (Results [0]);
    20. }
    21. /// <Remarks/>
    22. Public VoidHelloworldasync (){
    23. This. Helloworldasync (Null);
    24. }
    25. /// <Remarks/>
    26. Public VoidHelloworldasync (ObjectUserstate ){
    27. If((This. Helloworldoperationcompleted =Null)){
    28. This. Helloworldoperationcompleted =NewSystem. Threading. sendorpostcallback (This. Onhelloworldoperationcompleted );
    29. }
    30. This. Invokeasync ("Helloworld",New Object[0],This. Helloworldoperationcompleted, userstate );
    31. }
    32. Private VoidOnhelloworldoperationcompleted (ObjectArg ){
    33. If((This. Helloworldcompleted! =Null)){
    34. System. Web. Services. Protocols. invokecompletedeventargs invokeargs = (system. Web. Services. Protocols. invokecompletedeventargs) (ARG ));
    35. This. Helloworldcompleted (This,NewHelloworldcompletedeventargs (invokeargs. Results, invokeargs. error, invokeargs. cancelled, invokeargs. userstate ));
    36. }
    37. }
    38. /// <Remarks/>

4. Run the CSC command to generate service. CS as a DLL file.

5. add reference to this DLL in the project

6. In this way, the DLL application actually accesses our published Web through this proxy class.
Service.

 

Application of Design Patterns

The implementation of this method is the famous "proxy mode" in the design mode.

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.