Building the Web Service
Open Visual Studio.Net and select to create a new C# ASP.NET Web service. Name the service NWind.
Make sure that the Service1.asmx.cs[Design] window is selected. Open the 'Server Explorer' window, and browse to the SQL Servers node. Open the SQL Server that corresponds to COMPUTERNAME\VSDOTNET. Open the 'Tables' node and drag the 'Products' table onto the design window. Two objects should be present on the form, SqlConnection1 and SqlDataAdapter1.
Go to the 'Data' menu, and select 'Generate Dataset.' Select 'New' and call your dataset 'DataSet1'. Make sure that 'Add this dataset to the designer' is selected. Click OK.
This creates a dataset schema called DataSet1 and a dataset object called dataSet11.
Adding the following Web method exposes the contents of a dataset as an XML document:
public System.Xml.XmlDocument GetProductsData()
System.IO.StringWriter sw = new System.IO.StringWriter();
dataSet11.Namespace = "";
System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
You now have a Web service called NWind that exposes the Products table of the Northwind database as a Web service. It is relatively simple to expand this Web service to allow different tables, views, or stored procedures to be exposed or parameterized. For the sake of simplicity and demonstration, I am exposing the entire products table in this article.
Expand the Chart Server to Consume SOAP Web Services
Before you can expand the functionality of the chart server, there are three preparatory tasks:
- You need to change the structure of the chart server's configuration file. The chart server is taken from a configuration file called charts.xml. This file always assumes that a straight http call is being made to an XML data source. To allow the file to determine whether the call is a straight http call or a SOAP call, use the code example shown below. This new configuration structure allows dynamic Web service end points:
Notice that a new attribute, called 'type' is added to the chart node, and in the case of type='soap', two new nodes 'soapaction' and 'function' are required. These are explained in more detail below.
<chart id='soaptest' type='soap'>
- Download and install the SOAP toolkit. The chart server uses the SOAP toolkit to connect to and consume Web services.
Once the toolkit is installed, load the chart server you created in the previous article. In it, add a reference to the MS SOAP toolkit by selecting 'References' in the solution explorer and then selecting 'Add Reference'. On the ensuing dialog box, select the COM tab. Double click the 'Microsoft SOAP Type Library v3.0' and click 'OK'.
- Update the chart server source code to use the SOAP library and to understand the new XML format from Step 1. The source code for this article contains updated code for the following functions:
- Page_Load: This function now contains additions for processing the configuration file, reading in the extra nodes ('soapaction' and 'function' listed above), and reading the call type parameter.
- CallCharter: This function has been amended to allow extra parameters to be passed. In addition, depending on the call type, it calls a new function 'xmlSoapLoad'. This function consumes the Web service and returns an XML document to the charter.