Browse DevX
Sign up for e-mail newsletters from DevX


Serving Business Graphics from a Web Service  : Page 2

In this previous article, you built a VB.NET chart server that used XML and XPath to serve up business graphics. This article explains how, with the help of a couple extra tools, you can extend the chart server to consume and display results specifically from a Web service.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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:

[WebMethod] public System.Xml.XmlDocument GetProductsData() { sqlDataAdapter1.Fill(dataSet11.Products); System.IO.StringWriter sw = new System.IO.StringWriter(); dataSet11.Namespace = ""; dataSet11.WriteXml(sw); System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument(); xDoc.LoadXml(sw.ToString()); return xDoc;

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:

  1. 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:

    <url>http://localhost/nwind/service1.asmx/GetProductsData?</url> <config>http://localhost/dfconfig2.xml</config> <series>//ProductsDataSet/Products[UnitPrice>40]/./UnitsInStock</series> <categories>//ProductsDataSet/Products[UnitPrice>40]/./ProductName</categories> <XSize>600</XSize> <YSize>400</YSize> </chart> <chart id='soaptest' type='soap'> <url>http://localhost/Nwind/Service1.asmx?WSDL</url> <config>http://localhost/dfconfig2.xml</config> <series>//UnitPrice</series> <categories>//ProductName</categories> <XSize>600</XSize> <YSize>400</YSize> <soapaction>http://tempuri.org/GetProductsData</soapaction> <function>GetProductsData</function> </chart>

    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.
  2. 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'.

  3. 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.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date