Article

.NET Web Services Made Easy

Page: 1 2 3 4 5 6 Next

Consuming Our Web Service

Now we've created a simple Web Service, let's look at consuming it.

For someone to use a Web Service, they have to make a proxy client, which is then allowed access to all the exposed WebMethods. This can be coded by hand, but the .NET Framework includes a tool that will do this for you. We'll look at that shortly; the first thing we have to do is get ready to consume the Web Service.

The first thing we have to do is set up a new folder for the consumer site, so in your Web root (usually c:\inetpub\wwwroot\) create a folder called consumerSite. Now, enter that folder and make a folder called bin.

993_ws6

Now we'll look at creating our proxy class.

WSDL.exe

This is a command-line utility (more information on the command-line at WebmasterBase.com) that's used to generate proxy classes using any of the useable protocols. WSDL.exe assesses the Web Service and generates a proxy class in VB.NET, C# or Jscript, which then compiles the class into a DLL that's available to the consumer site.

Let's create our proxy class now. Open the command prompt and type:

wsdl http://localhost/quoteWS/quote.asmx?wsdl /l:vb /n:Quote      
/nologo /out:C:\inetpub\wwwroot\consumerSite\bin\quote_proxy.vb

993_ws7

If you browse to the bin directory, you'll find the file quote_proxy.vb! Cool, eh? You can find out more about the WSDL tool by entering this:

wsdl /?

Now we need to compile it into a DLL, so the site can use it:

vbc /t:library /out:C:\inetpub\wwwroot\consumerSite\bin\quote.dll      
/r:System.Web.Services.dll, System.dll, system.xml.dll, system.data.dll      
c:\inetpub\wwwroot\consumerSite\bin\quote_proxy.vd

Consuming The randomQuote() WebMethod

Ok, now that we've created our proxy class for the Web Service, we can look at actually consuming it!

The first WebMethod we're going to consume is the randomQuote(). Open your text editor and enter this code:

<%@ Page Language="VB" %>    
<%@ Import Namespace="System.Data" %>    
<%@ Import Namespace="Quote" %>    
   
<script language="VB" runat="server">    
 Sub Page_Load(ByVal obj As Object, ByVal e As EventArgs)    
   If ( NOT isPostBack ) Then    
     generateQuote(obj, e)    
   End If    
 End Sub    
     
 Sub generateQuote(ByVal obj As Object, ByVal e As EventArgs)    
   Dim randomQuote As New quote.quote()    
       
   Dim getQuote As DataSet = randomQuote.randomQuote()    
       
   returnQuote.DataSource = getQuote    
   returnQuote.DataBind()    
 End Sub    
     
 Function checkWebSite(byVal submittedBy As String,      
 byVal website As String) As String    
   Dim returnString As String    
       
   If ( Regex.isMatch(website, "\b(www|http)\S+\b") ) Then    
     returnString = Regex.Replace(website,      
     "\b(www|http)\S+\b", "<a href=""$0"" target=""_blank"">"      
     & submittedBy & "</a>")    
   Else    
     returnString = submittedBy    
   End If    
       
   Return returnString    
 End Function    
</script>    
<html>    
 <head>    
   <title>Quote Web Service!</title>    
   <meta http-equiv="Content-Type" content=    
   "text/html; charset=iso-8859-1">    
   <style>    
     .body { font-family: Verdana, Arial, Helvetica; font-size: 11; }    
   </style>    
 </head>      
 <body>    
   <form name="form1" runat="server">    
     <ASP:Repeater ID="returnQuote" runat="server">    
       <itemTemplate>    
         <table width="500" style="border:      
         1 solid #8080C0" class="body">    
         <tr>    
           <td>Quote Number: <%# DataBinder.Eval    
           (Container.DataItem, "quoteID") %></td>    
         </tr>    
         <tr>    
           <td>    
             <%# DataBinder.Eval(Container.DataItem,      
             "displayContent") %>    
           </td>    
        </tr>    
        <tr>    
           <td>    
             <%# checkWebSite(DataBinder.Eval    
             (Container.DataItem, "submittedBy"), DataBinder.Eval    
             (Container.DataItem, "website")) %>    
          </td>    
        </tr>    
      </table>    
      </itemTemplate>    
     </ASP:Repeater>    
     <br>    
     <ASP:LinkButton id="newQuoteButton" runat="server"      
     OnClick="generateQuote" Text="Get Another!" CssClass="body" />    
   </form>    
 </body>    
</html>

Save this file as default.asopx, browse along to the consumerSite URL, and you should see something like this:

993_ws8

How cool is that! Let's go over the code.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links