Consuming a DataSet using Web services is trickier than creating the Web service itself. There are a number of actions you must perform in order.
First, for your code to access the remote objects and methods, you must build a proxy class. A proxy class is a local set of methods that match the remote methods. Its purpose is to handle the work of actually calling the Web service and receiving the data. In other words, when you call the Web service in your code, you are actually calling the methods in the proxy class. The proxy class connects to the remote Web service on your behalf and automatically parses the payload of XML data returned from the service into values that your application can use directly.
To create a proxy class, go to a command prompt and run the following line of code to generate a Visual Basic .NET proxy class:
wsdl /language:vb http://localhost/ado24hours/ch24/suppliersvb.asmx namespace:Northwind
or this one to generate a C# proxy class:
wsdl /language:cs http://localhost/ado24hours/ch24/suppliersCS.asmx namespace:Northwind
The wsdl.exe utility connects to the Web service, analyzes its properties and methods, and constructs the local proxy class mentioned earlier.
The wsdl.exe utility connects to the remote machine to create the proxy class. You must have TCP/IP connectivity to port 80 of the machine hosting the Web service for the utility to work.
After you've run the utility, it generates either a .cs or .vb file, depending on the language you've chosen . The next step is to compile the proxy class. This is done using the standard C# or Visual Basic .NET compiler. To compile the proxy class in C#, type the following line into a command prompt:
csc /Target:library Suppliers.cs
To compile using Visual Basic .NET, use the following:
vbc /Target:library suppliers.vb /r:system.dll r:system.data.dll /r:system.xml.dll /r: system.web.services.dll
After you've compiled the proxy class into a DLL assembly, place that DLL into the /bin directory of your Web application. Now the remote Web method is accessible to any Web form in your project.
The last remaining step is to create a Web form that will make use of the remote method. The GetAllSuppliers method returns a list of suppliers, so you can bind this list to a DataGrid for display on the Web form. The DataSet object returned from the Web service is no different than any returned from a direct call to our data source.
In Listing 24.3, notice that you must import the Northwind namespace in order to have access to the Suppliers class. An instance of the Suppliers class is created in the LoadDataGrid() method. Then the GetAllSuppliers( ) method is called just as if it were a local method.
<% @Page debug="true" Language="VB" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <%@ Import Namespace="Northwind" %> <HTML> <HEAD> <LINK rel="stylesheet" type="text/css" href="Main.css"> <!-- End Style Sheet --> <script language="VB" runat="server" > Sub Page_Load(Source as Object, E as EventArgs) LoadDataGrid(orders) End Sub Private Sub LoadDataGrid( _ myDataGrid as System.Web.UI.WebControls.DataGrid) Dim mySuppliers as new Suppliers() orders.DataSource = mySuppliers.GetAllSuppliers() orders.DataBind() End Sub </script> </HEAD> <BODY> <h1>Consuming Suppliers DataSet</h1> <hr> <form runat="server" id=form1 name=form1> <asp:DataGrid id="orders" runat="server"></asp:DataGrid> </form> <hr> </BODY> </HTML>
If you are using Visual Studio .NET to consume a DataSet from a Web service, you must perform one additional step. You must also add the Web reference to your project. To do this, right-click on References and choose Add Web Reference as shown in Figure 24.4.
This will load the Add Web Reference browser as shown in Figure 24.5. Enter the URL of your suppliers.asmx Web service into the Address box. After you click Add Reference, you'll be able to reference the methods of the supplier's Web service.
When the Web form in Listing 24.3 loads, you will see a screen like the one in Figure 24.6.