Working with Web Servers


You can easily create desktop applications that access Google Web Services. In many cases, a desktop application is the right solution. However, you might find that a Web application can do the job more efficiently or with fewer problems. For example, your company might decide that it needs to support more than one platform. In this case, a Web application is a good answer because you can write a single application to support multiple platforms. This chapter can't provide a full discussion of the merits of using Web applications versus desktop applications, but both have their place. The following sections provide details on using ASP.NET to create a Google Web Services application for your IIS server.

Creating an ASP.NET Application

Visual Studio .NET makes it relatively easy to create an ASP.NET application based on the same code you might use for a desktop application. In fact, I often use my desktop trial application as a starting point for the ASP.NET version because the desktop version is easier to debug. However, you can't say the two environments are precisely the same ”the code is different, as is the application setup. Let's begin with the application setup.

You can use a DataGrid control for your output, just as you would for a desktop application. However, the DataGrid requires some special configuration. Microsoft assumes you want to automatically generate the columns for the DataGrid , but that strategy won't work, in this case, because some data isn't rendered correctly using the default setup. Set the Auto-GenerateColumns property to False. Click the ellipses button on the Columns property field and you'll see a dgGoogle Properties dialog box similar to the one shown in Figure 6.13.

click to expand
Figure 6.13: Use the dgGoogle Properties dialog box to configure the DataGrid columns.

Notice the list of fields in the Available Columns list. These fields appear in the dsGoogle DataSet object. Moving one of these entries to the Selected Columns list produces a text-only version of that data without much in the way of formatting. This field type works for three of the entries, but you really need a HyperLink Column object for the URL. Move a blank column to the Selected Columns list and configure it as shown in Figure 6.13. You must provide the Text Field and URL Field entries as a minimum, but I suggest that you also provide the Sort Expression field entry to ensure the user can sort on this column. Now the URL data will appear as an URL, rather than as text. You can use other column types for other purposes. For example, I often use a Button Column object for Boolean fields.

The DataGrid object is just one example of the special configuration issues you need to consider when using ASP.NET versus a desktop application. Most of the special controls that ASP.NET provides will require such configuration. Now that the application is configured, it's time to discuss some code. Listing 6.8 shows a typical example of an ASP.NET application for searching with Google Web Services. You'll find the complete source for this example in the \ Chapter 06 \ ASP_NET_Example folder of the source code located on the Sybex Web site.

Listing 6.8: Defining an ASP.NET Search
start example
 private void btnRequest_Click(object sender, System.EventArgs e)   {      GoogleSearchService Service; // Search service routine access.      GoogleSearchResult  Result;  // All of the results.      ResultElement[]     Items;   // All of the search items.      DataRow             DR;      // Output data.      // Create the search service.      Service = new GoogleSearchService();      // Make the call.      Result = Service.doGoogleSearch(txtLicense.Text, txtKey.Text,                                      0, 10, false, "", false, "",                                      "", "");      // Process the main nodes.      txtEstResults.Text = Result.estimatedTotalResultsCount.ToString();      txtEstResults.Visible = true;      lblEstResults.Visible = true;      // Clear the dataset of previous results.      dsGoogle.Tables["SearchResults"].Clear();      // Process the result elements.      Items = Result.resultElements;      foreach (ResultElement Item in Items)      {         // Add a row.         DR = dsGoogle.Tables["SearchResults"].NewRow();         // Add the data to the row.         DR["Title"] = StringToText(Item.title);         DR["URL"] = Item.URL;         if (Item.snippet.Length > 0)         DR["SnippetOrSummary"] = StringToText(Item.snippet);         else         DR["SnippetOrSummary"] = StringToText(Item.summary); DR["CachedSize"] = Item.cachedSize;         // Display the row on screen.         dsGoogle.Tables["SearchResults"].Rows.Add(DR);      }      // Make the DataGrid and associated label visible.      lblResults.Visible = true;      dgGoogle.Visible = true;      // Bind the DataGrid to the DataSet.      dgGoogle.DataBind();   } 
end example
 

As you can see from the source code, many of the mechanics of working with Google Web Services are the same whether you create a desktop or Web-based application. For example, you still need to create a Web reference to access Google Web Services and the code still creates a client that uses the doGoogleSearch() method to make the call. However, you should notice some interesting differences as well.

One of the more important differences is that the application doesn't automatically display the output fields. Notice that most of these fields are invisible and you must set the Visible property to True at runtime. I've found that this practice tends to reduce user confusion and enhances application performance, especially when a user relies on a dial-up connection.

Another issue to consider is that you must bind the DataGrid to the DataSet , even though you set DataSource and DataMember properties during setup. Binding occurs when the code calls dgGoogle.DataBind() . You must perform the data binding process every time the data in the DataSet changes. This means calling dgGoogle.DataBind() when you perform tasks such as clearing the DataSet or modify the DataSet as part of a caching operation. Although the mechanics of the application are the same, the output also differs . Figure 6.14 shows a typical example of the output from this application.

click to expand
Figure 6.14: ASP.NET can produce an application with an appearance similar to a desktop application.

Using the ASP.NET Applications in this Book

All of the ASP.NET applications in this book follow the same pattern as the example in the " Creating an ASP.NET Application" section of the chapter. You'll find two folders associated with every example on the Sybex Web site. The first folder, such as \ Chapter 06 \ ASP_NET_Example , contains the files that you should place on your development machine. The second folder, such as \ Chapter 06 \ ASP_NET_Example (Server) , contains the files that you should place on your Web server in the appropriate \Inetpub\ wwwroot folder.

Once you place the files on your system, open the SLN (Solution) file for the project using a plain text editor such as Notepad. The top of this file will contain several lines of information similar to the ones shown here:

 Microsoft Visual Studio Solution File, Format Version 8.00   Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ASP_NET_Example",   "http://winserver/0161/Chapter6/ASP_NET_Example/ASP_NET_Example.csproj" 

Change the URL on the third line to match the location of the files on your Web server. Once you make this change, save and close the file. When you open the SLN file, the Visual Studio .NET will automatically open the correct project files on your Web server. This technique lets you use a single machine by changing the Web server to localhost if desired. Make sure you recompile the application using the Rebuild Solution option to ensure that all of the compiled references also match your server setup.




Mining Google Web Services
Mining Google Web Services: Building Applications with the Google API
ISBN: 0782143334
EAN: 2147483647
Year: 2004
Pages: 157

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net