Recipe 11.2 Consuming a Web Service

     

11.2.1 Problem

You need to use a web service created by another group in your company to access data your application requires.

11.2.2 Solution

Add a web reference to an existing ASP.NET project using Visual Studio .NET. Create an instance of the web service class in your application and call its methods .

To add a web reference to an ASP.NET project in Visual Studio .NET:

  1. Select the project in the Solution Explorer, and then select Add Web Reference from the Project menu.

  2. In the Add Web Reference dialog box, enter the URL of the web service you want to consume , as shown in Figure 11-3, and then click the Add Reference button.

Visual Studio .NET will create all of the files needed to consume the web service.

After adding the web reference, create an instance of the web service class and call its methods in the code-behind class for the page. Example 11-8 through Example 11-10 show the .aspx file and VB and C# code-behind files for an example we've written to create an instance of the web service class from Recipe 11.1 and call its methods.

Figure 11-3. Adding a web reference
figs/ancb_1103.gif

11.2.3 Discussion

Visual Studio .NET makes consumption of a web service a trivial task by creating all of the plumbing for you. You don't have to worry about creating proxy classes and the SOAP messages; it's all done for you when you add a web reference.

In our example that illustrates the solution, the web service created in Recipe 11.1 is used to obtain a list of books and display the list in a DataGrid . We have accomplished this by writing just a few lines of code.

The first step in consuming a web service is to add a web reference to your project by selecting the project in the Solution Explorer, and then selecting Add Web Reference from the Project menu or by right-clicking the project in the Solution Explorer and selecting Add Web Reference from the context menu. You need to enter the URL of the web service you want to consume. This is normally the full URL of the .asmx file or the WSDL file of the web service. But when the web service resides on your own computer, as is the case with our example for this recipe, a URL like the following will do:

http://localhost/ASPNetCookbook/VBWebServices/CH11QuickWebServiceVB2.asmx

When adding a web reference be sure to select Add Web Reference, not Add Reference. Add Reference is used to add a reference to an assembly on the local server. Add Web Reference is used to add a reference to a web service.


After entering the URL of the .asmx or WSDL file of the web service, the Add Web Reference dialog box displays the operations provided by the web service in the left pane.

The web reference name should be changed to remove the tight coupling to a specific server, because the server hosting the web service can always change. For our example, we have renamed the web reference to ExampleWebServices , as shown in Figure 11-3.

When you click Add Reference, Visual Studio .NET creates and adds to your project all of the files needed to make a web service available to your application, including a disco file, which helps the application locate the service, and a WSDL file, which defines the services available from the web service. It then creates a proxy class that you use to access the web service. (The proxy class interfaces with the web service and provides a local representation of the service.) The web reference is given the name provided in the Add Web Reference dialog box and the proxy class is named Reference , as shown in Figure 11-4.

Figure 11-4. The default web reference in Solution Explorer
figs/ancb_1104.gif

After you add a web reference to the service you plan to employ , you need to create an instance of the proxy class. For instance, here's how we create an instance of the proxy class for the example web service in Recipe 11.1:

 
figs/vbicon.gif
 bookServices = New ExampleWebServices.CH11QuickWebServiceVB2 
figs/csharpicon.gif
 bookServices = new ExampleWebServices.CH11QuickWebServiceCS2( ); 

In our example, the getBookList method of the proxy class is then called to obtain a DataSet containing the book list. Even though this method call looks like a standard method call, the proxy class is actually calling the web service to get the data.

 
figs/vbicon.gif
 books = bookServices.getBookList(NUMBER_OF_BOOKS) 
figs/csharpicon.gif
 books = bookServices.getBookList(NUMBER_OF_BOOKS); 

For our example, the DataSet returned by the web service is bound to a DataGrid on the form to display the list of books.

 
figs/vbicon.gif
 dgBooks.DataSource = books dgBooks.DataBind( ) 
figs/csharpicon.gif
 dgBooks.DataSource = books; dgBooks.DataBind( ); 

By creating all of the plumbing required to access web services, which would be both tedious to write and error prone if you had to do it yourself, Visual Studio .NET makes using web services in your applications much more practical. This is particularly true if the web service provider and consumer are both .NET implementations , because most of the data types provided by the Common Language Runtime (CLR) can be used in the web service interfaces. (See the .NET Web Service Idiosyncrasies sidebar in Recipe 11.3 for more on this topic.)

Web services provided by other technology platforms, such as Java, can be consumed by .NET applications. Java and other platforms do not have a set of rich data types that match the CLR data types, so the interface must be designed using simple data types. The details of consuming a web service from other technologies are beyond the scope of this book.

11.2.4 See Also

If you need to use web services created with Java, see Java Web Services , by Dave Chappell and Tyler Jewell (O'Reilly), for information

Example 11-8. Consuming a web service (.aspx)
 <%@ Page Language="vb" AutoEventWireup="false" Codebehind="CH11ConsumingAWebServiceVB.aspx.vb" Inherits="ASPNetCookbook.VBExamples.CH11ConsumingAWebServiceVB"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Consuming A WebService</title> <link rel="stylesheet" href="css/ASPNetCookbook.css"> </head> <body leftmargin="0" marginheight="0" marginwidth="0" topmargin="0"> <form id="frmConfiguration" method="post" runat="server"> <table width="100%" cellpadding ="0" cellspacing="0" border="0"> <tr> <td align="center"> <img src="images/ASPNETCookbookHeading_blue.gif"> </td> </tr> <tr> <td class="dividerLine"> <img src="images/spacer.gif" height="6" border="0"></td> </tr> </table> <table width="90%" align="center" border="0"> <tr> <td><img src="images/spacer.gif" height="10" border="0"></td> </tr> <tr> <td align="center" class="PageHeading"> Consuming a Web Service (VB) </td> </tr> <tr> <td><img src="images/spacer.gif" height="10" border="0"></td> </tr> <tr> <td align="center" class="MenuItem"> <!-- Minimal datagrid --> <asp:DataGrid id="dgBooks" runat ="server" BorderColor="000080" BorderWidth="2px" AutoGenerateColumns="True" width="100%" /> </td> </tr> </table> </form> </body> </html> 

Example 11-9. Consuming a web service code-behind (.vb)
 Option Explicit On Option Strict On '----------------------------------------------------------------------------- ' ' Module Name: CH11ConsumingAWebServiceVB.aspx.vb ' ' Description: This module provides the code behind for the ' CH11ConsumingAWebServiceVB.aspx page ' '***************************************************************************** Imports Microsoft.VisualBasic Imports System Imports System.Data Namespace ASPNetCookbook.VBExamples Public Class CH11ConsumingAWebServiceVB Inherits System.Web.UI.Page 'controls on the form Protected dgBooks As System.Web.UI.WebControls.DataGrid '************************************************************************* ' ' ROUTINE: Page_Load ' ' DESCRIPTION: This routine provides the event handler for the page load ' event. It is responsible for initializing the controls ' on the page. '-------------------------------------------------------------------------  Private Sub Page_Load(ByVal sender As System.Object, _   ByVal e As System.EventArgs) _   Handles MyBase.Load   Const NUMBER_OF_BOOKS As Integer = 10   Dim bookServices As ExampleWebServices.CH11QuickWebServiceVB2   Dim books As DataSet   If (Not Page.IsPostBack) Then   'create an instance of the web service proxy class   bookServices = New ExampleWebServices.CH11QuickWebServiceVB2   'get the books from the service   books = bookServices.getBookList(NUMBER_OF_BOOKS)   'bind the book list to the datagrind on the form   dgBooks.DataSource = books   dgBooks.DataBind( )   End If   End Sub 'Page_Load  End Class 'CH11ConsumingAWebServiceVB End Namespace 

Example 11-10. Consuming a web service code-behind (.cs)
 //---------------------------------------------------------------------------- // // Module Name: CH11ConsumingAWebServiceCS.aspx.cs // // Description: This module provides the code behind for the // CH11ConsumingAWebServiceCS.aspx page // //**************************************************************************** using System; using System.Data; namespace ASPNetCookbook.CSExamples { public class CH11ConsumingAWebServiceCS : System.Web.UI.Page { // controls on the form protected System.Web.UI.WebControls.DataGrid dgBooks; //************************************************************************ // // ROUTINE: Page_Load // // DESCRIPTION: This routine provides the event handler for the page // load event. It is responsible for initializing the // controls on the page. //------------------------------------------------------------------------  private void Page_Load(object sender, System.EventArgs e)   {   const int NUMBER_OF_BOOKS = 10;   ExampleWebServices.CH11QuickWebServiceCS2 bookServices = null;   DataSet books = null;   if (!Page.IsPostBack)   {   // create an instance of the web service proxy class   bookServices = new ExampleWebServices.CH11QuickWebServiceCS2( );   // get the books from the service   books = bookServices.getBookList(NUMBER_OF_BOOKS);   // bind the book list to the datagrind on the form   dgBooks.DataSource = books;   dgBooks.DataBind( );   }   } // Page_Load  } // CH11ConsumingAWebServiceCS } 



ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2006
Pages: 179

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