Back to Papers and Articles
Calling Webservices from ASP.NET: Example Google Web Service
Copyright 2003 Paragon Corporation   ( March 14, 2003)

What are Web services?

Web services is a fairly new technology that has been around for the past few years. Web services is a conduit for providing application services between disparate applications using eXtended Markup Language (XML). The web services technology we feel is finally beginning to mature a bit.

The idea of web services as a mechanism for bridging disparate systems - is not new. Web services is really just a slight twist of an old technology. It is basically Remote Procedure Calls (RPC) over HTTP.

Initially it was proscribed as the panacea to solve all IT application integration problems. This has not been fully realized because of many issues - such as lack of development tools at the on-set to support the infrastructure, security concerns, complexity, network speed and object serialization/deserialization processing speed. Nevertheless, web services are proving to be a very useful tool when used sparingly.

What's the big deal with Web Services?

The big deal with webservices is how it differentiates itself from prior standards such as DCOM, CORBA, Java Remote Method Invocation (RMI). In a nutshell - these standards were designed to perform a similar function to web services, but the problem with them is that interoperation between them was too complicated and too expensive. For example to integrate one system using DCOM with another using CORBA would require using a bridge.

Web services is a more widely accepted univeral standard. Java has a web services tool kit, ASP.NET has one, and so do other languages and environments. It's probably one of the first times that all big players have come to an agreement on a standard.

If Web services is so great and such an obvious solution - why didn't we do it 10 years ago?
The reason is simple. We couldn't. There is always a trade-off between standardization, speed and simplicity. Web services are inherently slower than other standards because of the need to convert an XML blob of text into an object. 10 years ago we could not afford this speed crunch, and also the need of interoperation between Joe here and Jim 20 miles away was not feasable because of network issues. Web services then was simply an answer to a problem that was way down on the totem pole of our concerns.

Does web services completely replace DCOM, CORBA, or Java RMI?
- NO. The reason is very simple. Each of the aforementioned standards - while difficult to integrate with other standards -- work well in the environments they were designed for and are highly optimized for those environments. E.g if all your apps are in DCOM - it makes sense to integrate them with DCOM, but if you need to collaborate with your neighbor next door and you don't know what he uses - go with an open-standard such as Web Services for that integration.

What makes up a Web Service?

The current web services standard as defined by the W3C org is composed of 4 main parts.
  • The service which is usually performed by a web server that listens for requests of the service. This is generally just a web like page or cgi script that gets posted to similar to the way other http web requests are done. Instead of returning HTML though it returns a formalized XML message in Simple Object Application Protocol (SOAP) format etc.
  • The service description - specified in Web Services Description Language (WSDL) - this description defines the web methods (functions) that a service will accept - the inputs that go into these methods, and the format of the output that can be expected in return. It is the contract or message signature of the service. This is what you will need to generate a web service client proxy.

    The .NET SDK comes with a utility WSDL.exe that will autogenerate a client proxy class in .vb or C# given a .wsdl file.
  • The web service registry This piece is basically a directory of web services. The current standard is Universal Description, Discovery, and Integration (UDDI). This is an optional piece because a web service need not be listed in a UDDI registry to be used. The registry is just a nice way of cataloging available services - similar to what Java Naming and Directory Service (JNDI) does for Java.

  • Finally there is the web service client proxy - which negotiates the communication between a web service and a client. It accepts the inputs and requests - passes it off to the web service - waits for the service to respond and then converts the response to something meaningful. This is known as serialization/deserialization - conversion from the web service XML response back to an object and conversion of a user request to XML format required by the service.

Google Web service

Incorporating a search engine seamlessly into ones site is a feature very commonly needed. The new Google Web Service (currently in beta) and the ample support in ASP.NET for web services - makes this a snap. In this article we detail how to call this particular service in ASP.NET.

Setting up

  1. Download the Google API from and then follow the instructions to acquire a search key.
  2. Get the >NET SDK if you don't have it already and don't have VS.NET
  3. You can use the packaged Google api web service proxy or generate your own proxy class using the wsdl.exe util that comes packaged with ASP.NET SDK.
  4. Compile the proxy class using a command something like
    vbc /t:library /out:Google.dll Google.vb /r:System.web.dll /r:System.web.Services.dll / /r:system.dll /r:System.xml.dll
    Note that for this we renamed the generated proxy class file to Google.vb mostly for clarity.
  5. Next copy the generated Google.dll into the bin folder of your web app.
  6. Add an entry in Web.config to hold the google key you received -- in ours we put it in the appsettings section of web.config and called the key googlekey. Alternatively, you can hard-code the key in the search page.
  7. Write the search page - a sample of our code is shown in the next section

See it in Action

Below is the sample we set up.
Search Paragon The main sections of the code are as follows - this is in VB.Net format

<%@ Page Language="VB" %>
<script runat="server" >
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Request("Searchterm") > "" Then
       End If
    End Sub
    Private Sub btnSearch() 
        ' Create a Google Search object
        Dim s As New Google.GoogleSearchService()
        Dim relements() As Google.ResultElement
        Dim item As Google.ResultElement
            ' Invoke the search method
            Dim r As Google.GoogleSearchResult = s.doGoogleSearch(ConfigurationSettings.AppSettings("googlekey"),  _
Request("SearchTerm") & "", _
0, 10, True, "", False, "", "", "")
            ' Extract the estimated number of results for the search and display it
            Dim estResults As Integer = r.estimatedTotalResultsCount 
            lblSearchResults.Text = "" & CStr(estResults & " search results") & "" 
            relements = r.resultElements
            dgSearch.DataSource = relements
        Catch ex As System.Web.Services.Protocols.SoapException
        End Try
    End Sub

'----HTML code snippet section -- we use a data list to load - but a datagrid can be used just as easily
<table cellpadding="0" cellspacing="5" border="0" width="690px">
		<td valign="top" align="right" nowrap>
			<form method="post" runat="server">
<b>Search: </b> <input type="text" name="SearchTerm" />
<input type="submit" value="GO" class="button"/></form>
		<td valign="top">
	             <asp:Label id="lblSearchResults" runat="server"></asp:Label>
	             <asp:DataList id="dgSearch" runat="server" valign="top">
                        <th align="left">Title</th>
	                <ItemStyle cssclass="result" />
	                <AltItemStyle cssclass="result" bgcolor="EEEEEE" />
                        <td align="left">
<a href="<%#Container.DataItem.url%>" target="_blank"><%# Container.DataItem.title %></a></td>
                    <tr><td><font class="body1"><%#Container.DataItem.snippet%><br>
<font size=-2><%#Container.DataItem.url%></font></td></tr>


Links of Interest

Example of Google Service in PHP This provides a sample script of using Google Search Service in PHP
Using the Google web service in Macromedia Flash MX This article is a full tutorial on how to use Google Webservices in Macromedia Flash and Cold Fusion. The source code included includes the flash files, Dreamweaver template and Cold Fusion components needed for the tutorial. Web Services Index page This page is an index to current W3C activity on Web Services standards and works. This is the page to look to find out what the latest standards are in Web services and what is planned for the future. It gets into fairly technical detail.

Back to Papers and Articles