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:
-
- [Webmethod]
-
- Public dataset executequery (string SQL)
-
- {
-
- Using (oracleconnection conn = new oracleconnection (connstrsql ))
-
- {
-
- Try
-
- {
- Conn. open ();
-
-
- Oraclecommand cmd = new oraclecommand (SQL, Conn );
-
-
- Oracledataadapter Ada = new oracledataadapter (CMD );
-
- Dataset DS = new dataset ();
-
- Ada. Fill (DS );
-
- Return Ds;
-
- }
-
- Catch (oracleexception ex)
-
- {
-
- Throw new exception (ex. Message );
-
- }
-
- Finally
- {
-
- Conn. Close ();
-
- }
-
-
- }
Client:
-
- /// Description:
-
- /// A graph with the words Dynamically Loaded in Panel1
-
- ///
-
- Public partial class frmstock: Form
-
- {
- Service. myservice W = new service. myservice (); // call the Web Service
-
-
- Public frmstock ()
-
- {
-
- Initializecomponent ();
-
- }
-
-
- Private void frmstock_load (Object sender, eventargs E)
-
- {
-
- // Add an Asynchronous Method to trigger an event after execution
- W. executequerycompleted + = new WMS. executequerycompletedeventhandler (w_executequerycompleted );
-
-
- }
-
-
- Private void btnfind_click (Object sender, eventargs E)
-
- {
-
- String SQL = "select * from test ";
-
-
- W. executequeryasync (SQL); // call the corresponding Asynchronous Method
-
- Changestatus (true );
-
-
- }
-
-
- Void w_executequerycompleted (Object sender, WMS. executequerycompletedeventargs E)
-
- {
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:
-
- [Webmethod]
-
- Public dataset executequery (string SQL)
-
- {
- Using (oracleconnection conn = new oracleconnection (connstrsql ))
-
- {
-
- Try
-
- {
-
- Conn. open ();
-
-
- Oraclecommand cmd = new oraclecommand (SQL, Conn );
-
- Oracledataadapter Ada = new oracledataadapter (CMD );
-
- Dataset DS = new dataset ();
-
- Ada. Fill (DS );
-
-
- Return Ds;
-
- }
- Catch (oracleexception ex)
-
- {
-
- Throw new exception (ex. Message );
-
- }
-
- Finally
-
- {
-
- Conn. Close ();
- }
-
-
- }
Client:
-
- /// Description:
-
- /// A graph with the words Dynamically Loaded in Panel1
-
- ///
-
- Public partial class frmstock: Form
-
- {
-
- Service. myservice W = new service. myservice (); // call the Web Service
-
-
- Public frmstock ()
-
- {
- Initializecomponent ();
-
- }
-
-
- Private void frmstock_load (Object sender, eventargs E)
-
- {
-
- // Add an Asynchronous Method to trigger an event after execution
-
- W. executequerycompleted + = new WMS. executequerycompletedeventhandler (w_executequerycompleted );
-
-
- }
-
- Private void btnfind_click (Object sender, eventargs E)
-
- {
-
- String SQL = "select * from test ";
-
-
- W. executequeryasync (SQL); // call the corresponding Asynchronous Method
-
-
- Changestatus (true );
-
-
- }
-
-
- Void w_executequerycompleted (Object sender, WMS. executequerycompletedeventargs E)
- {
-
- Datatable dt = E. Result. Tables [0]; // obtain execution result E
-
- Changestatus (False);
-
- If(Dt. Rows. Count <= 0)
-
- {
-
- MessageBox. Show ("No data","Information", Messageboxbuttons. OK, messageboxicon. information );
- Return;
-
- }
-
- Gvshow. autogeneratecolumns =False;
-
- Gvshow. datasource = DT;
-
-
- }
-
- Private VoidBtnexit_click (ObjectSender, eventargs E)
-
- {
-
- This. Close ();
-
- }
-
-
- Private VoidChangestatus (BoolSign)
- {
-
- Panel1.visible = sign;
-
-
- }
-
- }
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)
-
- /// <Remarks/>
- Public EventHelloworldcompletedeventhandler helloworldcompleted;
-
-
- /// <Remarks/>
-
- Public EventShowinfocompletedeventhandler showinfocompleted;
-
-
- /// <Remarks/>
- [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)]
-
- Public StringHelloworld (){
- Object[] Results =This. Invoke ("Helloworld",New Object[0]);
-
- Return((String) (Results [0]);
-
- }
-
-
- /// <Remarks/>
- PublicSystem. iasyncresult beginhelloworld (system. asynccallback callback,ObjectAsyncstate ){
-
- Return This. Begininvoke ("Helloworld",New Object[0], callback, asyncstate );
-
- }
-
-
- /// <Remarks/>
- PublicStringEndhelloworld (system. iasyncresult asyncresult)
-
- {
-
- Object[] Results =This. Endinvoke (asyncresult );
-
- Return((String) (Results [0]);
-
- }
-
- /// <Remarks/>
-
- Public VoidHelloworldasync (){
-
- This. Helloworldasync (Null);
-
- }
-
-
- /// <Remarks/>
- Public VoidHelloworldasync (ObjectUserstate ){
-
- If((This. Helloworldoperationcompleted =Null)){
-
- This. Helloworldoperationcompleted =NewSystem. Threading. sendorpostcallback (This. Onhelloworldoperationcompleted );
-
- }
- This. Invokeasync ("Helloworld",New Object[0],This. Helloworldoperationcompleted, userstate );
-
- }
-
-
- Private VoidOnhelloworldoperationcompleted (ObjectArg ){
- If((This. Helloworldcompleted! =Null)){
-
- System. Web. Services. Protocols. invokecompletedeventargs invokeargs = (system. Web. Services. Protocols. invokecompletedeventargs) (ARG ));
-
- This. Helloworldcompleted (This,NewHelloworldcompletedeventargs (invokeargs. Results, invokeargs. error, invokeargs. cancelled, invokeargs. userstate ));
- }
-
- }
-
-
- /// <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.