Using a Web Service as a Data Source

Problem

You want to use a web service as the data source for a client application.

Solution

Create a web service that returns a DataSet to a client, and then invoke the web service from the client to retrieve the DataSet .

The XML web service code contains one method:

LoadOrders( )

Creates a DataSet containing all Orders and Order Details data from Northwind. A DataRelation is created relating the tables. The DataSet is returned by the method.

The client-side code instantiates the web service class and calls the LoadOrders( ) method to create a DataSet containing the Orders and Order Details data from Northwind. The default view of the Orders table is bound to a data grid on the form.

The C# code for the XML web service is shown in Example 2-4.

Example 2-4. File: NorthwindServiceCS.asmx.cs

// Namespaces, variables, and constants using System; using System.ComponentModel; using System.Web.Services; using System.Configuration; using System.Data; using System.Data.SqlClient; public const String ORDERS_TABLE = "Orders"; public const String ORDERDETAILS_TABLE = "OrderDetails"; public const String ORDERID_FIELD = "OrderID"; public const String ORDERS_ORDERDETAILS_RELATION = "Order_OrderDetails_Relation"; // . . . [WebMethod] public DataSet LoadOrders( ) { DataSet ds = new DataSet( ); SqlDataAdapter da; // Fill the Order table and add it to the DataSet. da = new SqlDataAdapter("SELECT * FROM Orders", ConfigurationSettings.AppSettings["DataConnectString"]); DataTable orderTable = new DataTable(ORDERS_TABLE); da.FillSchema(orderTable, SchemaType.Source); da.Fill(orderTable); ds.Tables.Add(orderTable); // Fill the OrderDetails table and add it to the DataSet. da = new SqlDataAdapter("SELECT * FROM [Order Details]", ConfigurationSettings.AppSettings["DataConnectString"]); DataTable orderDetailTable = new DataTable(ORDERDETAILS_TABLE); da.FillSchema(orderDetailTable, SchemaType.Source); da.Fill(orderDetailTable); ds.Tables.Add(orderDetailTable); // Create a relation between the tables. ds.Relations.Add(ORDERS_ORDERDETAILS_RELATION, ds.Tables[ORDERS_TABLE].Columns[ORDERID_FIELD], ds.Tables[ORDERDETAILS_TABLE].Columns[ORDERID_FIELD], true); return ds; }

The C# web services client-side code is shown in Example 2-5.

Example 2-5. File: WebServiceDataSourceForm.cs

// Namespaces, variables, and constants using System; using System.Windows.Forms; using System.Data; // Table name constants private const String ORDERS_TABLE= "Orders"; // . . . // Create the Web Service object. NorthwindServiceCS nws = new NorthwindServiceCS( ); // Load the DataSet containing orders and order details. DataSet ds = nws.LoadOrders( ); // Bind the default view of the orders table to the grid. dataGrid.DataSource = ds.Tables[ORDERS_TABLE].DefaultView;

Discussion

An XML web service is software that is accessible using Internet standards such as XML and HTTP. Because it is accessible through open -standard interfaces, web services make it easy to allow heterogeneous systems to work together.

.NET makes it very easy to build XML web services. In .NET, web services are implemented as .ASMX files beginning with a @WebService directive. For example, the solution code contains the following directive:

<%@ WebService Language="c#" Codebehind="NorthwindServiceCS.asmx.cs" Class="NorthwindServiceCS" %>

Methods in the web service class that are exposed over the Web are tagged with the WebMethod attribute; untagged methods can be used only internally by the web service. To deploy the web service, copy it to a virtual directory that has script execute permissions on an IIS web server that has ASP.NET support.

To use the web service class, use wsdl.exe to create the client-side proxy class. For the solution, the command is:

wsdl.exe http://localhost/NorthwindWebServiceCS/NorthwindServiceCS.asmx

Then, as with a local class, the client is able to instantiate the web service class using the new operator.

For more information about creating and consuming XML web services, see the MSDN Library.

The solution shows that there is very little difference between implementing the LoadOrders( ) methods to retrieve a DataSet containing the Orders and Order Details data from Northwind as a local class or as a web services class.

Категории