Asynchronous Web Service Invocation
The calls to the Google Web methods using the methods in the class auto-generated by Visual Studio .NET in Chapter 16 (and throughout
Building Research Tools with Google For Dummies) are synchronous. When you call a method synchronously, the program waits for the method to complete before it continues on and executes subsequent code. Generally, this makes sense -- because the Google Web service is very speedy.
Understanding Asynchronous Invocation
But suppose you can't count on a speedy return from a Web service. An example of this might be if you knew your users had
a very slow connection. In that case some other architecture might make more sense -- so your program isn't sitting, waiting for the call
to the Web service to finish. That's where asynchronous invocations comes in.
In an asynchronous method call does not block a program from continuing. Instead, processing continues, and the asynchronous method sends a message when it is done. Your program can then respond to that message in whatever way is appropriate.
Programming with asynchronous methods is a little more complicated than programming synchronously. The reason for using asynchronous method calls is you don’t want to hold everything up if you think the call to the method may take time.
As I just mentioned, Google makes a point of showing off its ability to respond to queries incredibly rapidly, so there’s usually no real need to call the Google Web service APIs asynchronously.
If you do need to call the Google APIs asynchronously, you should know that the class generated for the Google Web service contains asynchronous methods as well as the synchronous methods that I show you in this chapter. For example, the synchronous method doGoogleSearch sends a query to Google. The related asynchronous methods are BegindoGoogleSearch and EnddoGoogleSearch. You can see these methods by inspecting the code of the proxy class,
or by using the Object Browser, or by viewing the auto-completion options when you instantiate a Google class instance.
An Asynchronous Example
It's easiest to understand how to go about wiring an asynchronous call to the Google Web service with an example.
The example uses the same user interface as the Windows program in Chapter 16.
This program needs the user to input their developer key and a Google search term. When the Search button is clicked, the snippet associated with first
result is returned, along with the first result URL. In the original, synchronous version, all the action takes place in the click event of the Search button.
To call the Google Web service method asynchronously, first create a new procedure, ServiceCallBack, to hold the call back object that will be used
with the asynchronous call. Note that it takes an IAsyncResult as an argument:
public void ServiceCallBack (IAsyncResult ar)
{
}
Next, within the Search button's click event, create a variable, s, to hold an instance of
the Google Web service, and instantiate it:
- Google.GoogleSearchService s =
- new Google.GoogleSearchService();
Next, instantiate an AsynchCallback object, passing it the ServiceCallBack procedure as an argument,
which will be the location it sends it results back to:
AsyncCallback cb = new AsyncCallback(ServiceCallBack);
Now you can invoke the BegindoGoogleSearch method using its normal
arguments, with two additional arguments after the normal ones, the AsyncCallback object, and the instance of the
service class. The results are assigned to a IAsyncResult object:
- IAsyncResult ar = s.BegindoGoogleSearch (txtDevKey.Text,
- txtSearchTerm.Text, 0, 1, false, "", false, "", "", "", cb, s);
When your program executes this code, with one "ear" out it is waiting for
the call to the Google method to complete. In the meantime you can do anything else you'd like. For example:
lblSnippet.Text = "Whistle while you wait...";
- while (ar.IsCompleted == false)
- // do stuff waiting for the call to the Google Web service to complete
- {
- MessageBox.Show("You can do other stuff!");
- }
Meanwhile, back in the ServiceCallBack procedure, you need to add the code
that is executed when the Web method call is completed. The first step is to cast, or convert the
types of the IAsyncResult object that is returned to a Google Web service type:
- Google.GoogleSearchService s =
- (Google.GoogleSearchService) ar.AsyncState;
Using the EnddoGoogleSearch method, assign the results to a GoogleSearchResult object:
Google.GoogleSearchResult r = s.EnddoGoogleSearch(ar);
Now you can do whatever you would normally do with the results from a Google search:
if (r.resultElements.Length > 0)
- {
- lblSnippet.Text = r.resultElements[0].snippet;
- lblURL.Text = r.resultElements[0].URL;
- }
- else
- lblSnippet.Text = "No results! Please try another search.";
The complete code listing for the Search button click event and the related call back procedure is
shown below.
Code Listing: Asynchronous Google Web service method invocation
- public void ServiceCallBack (IAsyncResult ar)
- {
- Google.GoogleSearchService s =
- (Google.GoogleSearchService) ar.AsyncState;
- Google.GoogleSearchResult r = s.EnddoGoogleSearch(ar);
- if (r.resultElements.Length > 0)
- {
-
- lblSnippet.Text = r.resultElements[0].snippet;
- lblURL.Text = r.resultElements[0].URL;
- }
- else
-
- lblSnippet.Text = "No results! Please try another search.";
- }
- private void btnSearch_Click(object sender, System.EventArgs e)
- {
- try
- {
-
- // Create a Google Search object
- Google.GoogleSearchService s =
-
- new Google.GoogleSearchService();
- AsyncCallback cb = new AsyncCallback(ServiceCallBack);
- IAsyncResult ar = s.BegindoGoogleSearch (txtDevKey.Text,
-
- txtSearchTerm.Text, 0, 1, false, "", false, "", "", "", cb, s);
- lblSnippet.Text = "Whistle while you wait...";
- while (ar.IsCompleted == false)
- // do stuff while the call completes
- {
- MessageBox.Show("You can do other stuff!");
- }
- }
- catch (System.Web.Services.Protocols.SoapException ex)
- {
-
- MessageBox.Show(ex.Message);
- }
- catch (Exception excep)
- {
-
- MessageBox.Show (excep.Message);
- }
- }
Download the form class with the asynchronous call
Learn about C# programming
|
|
Search Engine Optimization
 
Syndication Viewer
Our Web host:
IX WebHosting
|