Fun Pet Trick 5: WSDL Files and the Code Directory


Fun Pet Trick #5: WSDL Files and the Code Directory

By Scott Guthrie

One of the nice new features of the code directory is that it now supports the ability to automatically generate a web service proxy class for you from a WSDL file. This enables you to quickly drag and drop a web service description file (with the extension .wsdl) into your web site—and immediately start to take advantage of it without having to run a proxy generation tool, or jump through any hoops to use it.

Suppose I wanted to use the Google search engine from within my ASP.NET application; I could simply download the Google WSDL file and save it as GoogleService.wsdl within my code directory, and then call the googleSearch.doGoogleSearch() method from within any of my pages or classes in order to invoke it. Visual Studio will of course provide full IntelliSense for me when going against this dynamically generated proxy class.

Following are the steps to see this in your own projects:

  1. Create a code directory underneath your web application.

  2. Copy the WSDL file called GoogleSearchService.wsdl underneath the code directory.

  3. Click the refresh button within the Solution Explorer if you've created this file outside the IDE to make sure the project system sees the file.

  4. Create a new VB page called Google.aspx, and paste in the code that follows to use it:

     <%@ page language="VB" %> <script runat="server">     Sub Button1_Click(ByVal sender As Object,     ByVal e As System.EventArgs)         Dim GoogleKey As String =         "/2NPhvVQFHLVehciRntdH0cJIVYJlrJS"         Dim googleService As New GoogleSearchService         Dim results As GoogleSearchResult =             googleService.doGoogleSearch(_                 GoogleKey,  TextBox1.Text, 0, 10, True, "", True, "", "", "")         DataList1.DataSource = results.resultElements         DataList1.DataBind()     End Sub </script> <html> <body>     <form  runat="server">         Search Term:         <asp:textbox  runat="server"></asp:textbox>         &nbsp;         <asp:button  runat="server" text="Button"             onclick="Button1_Click" />         <br />         <br />         <asp:datalist  runat="server">             <ItemTemplate>                 <asp:label  font-size="15pt" font-bold="true"                     text="<%# Container.DataItem.title %>"                     runat="server" />                 <br />                 <asp:label                      text="<%# Container.DataItem.snippet %>"                     runat="server" />                 <br />                 [<asp:hyperlink                      navigateurl="<%# Container.DataItem.URL %>"                     text="<%# Container.DataItem.URL %>"                     runat="server"/>]                 <br /><br />             </ItemTemplate>         </asp:datalist>     </form> </body> </html> 

  5. Click Run within Visual Studio.

  6. Enter a search term on the page, and then click the button to retrieve the results of that search term from Google. The results will then be displayed on the page by data binding them to an ASP.NET DataList control.

Note that the preceding code sample has a googlekey value. Visit this URL to register your own free account with Google and receive your own key (the one shown in the example is Scott's): https://www.google.com/accounts/NewAccount?continue=http://api.google.com/createkey&followup=http://api.google.com/createkey.

A pretty easy way to add search to your site . . . and a nice little demo of a cool new ASP.NET version 2.0 feature.




ASP. NET 2.0 Revealed
ASP.NET 2.0 Revealed
ISBN: 1590593375
EAN: 2147483647
Year: 2005
Pages: 133

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