Asynchronously call Web Services

Source: Internet
Author: User

For some requests, the process of calling webmethod and returning results takes a long time. Sometimes, the Web service that requests data from cannot be controlled, or the performance or response time of these services cannot be controlled, therefore, Asp.. NET applications can execute other programming tasks while their internal SOAP requests are waiting for responses. . NET application after completing other tasks, obtain results from the web service.

To use the Web Service asynchronously, you can use the beginxxx and endxxx methods. Specifically, XXX is your web method name. In addition, you can also use iscompleted to check whether the Web service has been completed. this is a method, which will be discussed in this article. in net2.0, another method for Asynchronously calling Web services is provided. When a new webmethod is created and the Web Service is referenced on the client, xxxasync can be seen in addition to the web method, xxx completed has two methods. The following two automatically generated methods are used to asynchronously call the Web Service:

First, create a new Web service to return all the records of the authors table of the pubs database in SQL Server2000, and call the Web Service asynchronously. The web service does not need to be changed. below is the C # code:

[Webmethod]
Public dataset ba_operationclassgetlist ()
...{
String strcon = "Persist Security info = false; initial catalog = pubs; user id = sa; Password =; server = 127.0.0.1 ";
String sqlstring = "select * from authors ";
Sqlconnection connection = new sqlconnection (strcon );
Dataset DS = new dataset ();
Connection. open ();
Sqldataadapter command = new sqldataadapter (sqlstring, connection );
Command. Fill (DS, "ds ");
Return Ds;
}

Next, I will explain how to call this Web Service asynchronously on the client (taking winfom as an example). First, add a reference to this Web Service. below is all the source code of the client:

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;

Namespace windowsapplication1
...{
Public partial class form1: Form
...{
Private localhost. WebService Service;
Public form1 ()
...{
Initializecomponent ();
}
Private void button#click (Object sender, eventargs E)
...{
// Start asynchronous call
Service. ba_operationclassgetlistasync ();
}
Private void service_ba_operationclassgetlistcompleted (Object sender, localhost. ba_operationclassgetlistcompletedeventargs E)
...{
Dataset DS = (Dataset) E. result;
Datagridview1.datasource = Ds. Tables [0];
}
Private void form1_load (Object sender, eventargs E)
...{
Service = new windowsapplication1.localhost. WebService ();
Service. ba_operationclassgetlistcompleted + = new windowsapplication1.localhost. ba_operationclassgetlistcompletedeventhandler (service_ba_operationclassgetlistcompleted );
}
}
}

At this point, the code for asynchronous use of web services has been written, and we can display a scroll bar or provide a Cancel button while waiting for the Web response to terminate the Web response at any time.

 

 

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.