DekGenius.com
[ Team LiB ] Previous Section Next Section

4.4 Consuming a Web Service

In ASP.NET, consuming a web service is nearly as easy as creating one. ASP.NET provides a utility called wsdl.exe that can create a proxy class, which is a class that knows all of the necessary details of communicating with the web service via SOAP, as shown in Figure 4-1, and which can be called from a client application the same way as any other managed class. In this way, the proxy class abstracts away the complexities of communication with the web service.

Consuming a web service in ASP.NET requires four steps:

  1. Locate the WSDL contract for the desired web service.

  2. Create a proxy class by using the wsdl.exe command-line utility.

  3. Compile the proxy class.

  4. Create a new instance of the proxy class in the client application (WinForms, Console, or ASP.NET) and call the desired methods.

In the case of our Qotd_cb web service, you would execute the following command (again, conveniently saved as a DOS batch file) to generate a proxy class based on the web service:

wsdl /l:vb /out:Qotd_cb_proxy.vb http://localhost/ASPdotNET_iaN/Chapter_ 4/Qotd_cb.asmx?WSDL
  
pause

The /l parameter specifies that the proxy class should be generated in Visual Basic .NET (the default is C#). The /out parameter specifies the name and, optionally, the path of the output file. This is important if you are compiling your proxy class in the same directory as the code-behind class that implements the web service. In this case, if you do not specify the output filename, the file Qotd_cb.vb will be overwritten. Once the proxy class has been generated, it should be compiled, and the resulting assembly should be placed in the bin directory. This can be accomplished using a command such as the one in the following snippet:

vbc /t:library /r:System.Web.dll,System.dll,System.Web.Services.dll, System.
Xml.dll,System.Data.dll /out:bin\qotd_cb_proxy.dll 
qotd_cb_proxy.vb
  
pause

Remember that all parameters for the vbc.exe compiler should be part of the same command; therefore, there should not be any line breaks if you choose to save the command to a batch file.

Once you've generated and compiled your proxy class, using the web service is exactly like using any other .NET class. You simply create an instance and call the desired methods. Example 4-5 shows the code for a simple ASP.NET page that consumes the Qotd_cb web service.

Example 4-5. Qotd_cb.aspx
<%@ Page Language="VB" %>
<%@ Import Namespace="aspnetian" %>
<html>
<head>
<title>Quote of the Day Web service example</title>
<script runat="server">
   Sub Page_Load(Sender As Object , e As EventArgs )
      Dim Quote As New Qotd_cb( )
      Dim QuoteNumber As Integer
      QuoteNumber = Quote.GetQuoteNumber
      Message1.Text = Quote.GetQuote(QuoteNumber)
      Message2.Text = Quote.GetAuthor(QuoteNumber)
   End Sub
</script>
</head>
<body>
   <h1>Demonstration of Quote of the Day Web service</h1>
   <form runat="server">
      <h4><i>"<asp:Literal id="Message1" runat="server" />"</i></h4>
      <h3>--<asp:Literal id="Message2" runat="server" /></h3>
   </form>
</body>
</html>

The page imports the aspnetian namespace defined in the Qotd_cb web service class, creates an instance of the proxy class, and then calls the GetQuoteNumber method to retrieve a random quote number. The page then calls the GetQuote and GetAuthor methods, passing in the quote number each time, and returns the result to the Text property of two ASP. NET literal controls. The output of this page is shown in Figure 4-7.

Figure 4-7. Qotd_cb.aspx output
figs/anet2_0407.gif
    [ Team LiB ] Previous Section Next Section