devxlogo

Serving Business Graphics from a Web Service

Serving Business Graphics from a Web Service

ost graphics packages require you to format your XML data to a specific standard before they can output any charts, a method which robs you of the flexibility that makes XML valuable in the first place. This loss is unnecessary, since it’s relatively easy to create a data agnostic charting application in which the data comes from any http source as XML, as demonstrated in my last article. In this article, I’ll show you how to take full advantage of XML’s flexibility. Using the same concepts and code, you’ll use SOAP to expand the chart server’s functionality to consume Web Services.

This article will walk you through the process. First, you will create a Web service that exposes SQL Server data from a sample NorthWind database via a Web service. Next, using the Microsoft SOAP Toolkit, you’ll enhance your charting server to consume the data and chart it. Lastly, you’ll build a sample bar chart showing all of the products in stock from the NorthWind database.

Preparation
To attach your application to a Web service in Visual Studio.NET, use the ‘Add Web Reference’ functionality within the project explorer window. This generates classes that implicitly handle the SOAP messaging between your application and the target Web service. This form of binding is very useful?but is limited. The software designer has to explicitly declare at design time the connection to the Web service.

For the chart server to be as flexible as the URL based one from the earlier article, the facility to discover and consume SOAP based Web services has to be done at run time. Visual Studio.NET is limited in how it can achieve this, but can be expanded on by downloading and installing the Microsoft SOAP toolkit. The toolkit supports the extended functionalities. I will walk you through this process later in the article.

If you haven’t done so already, you also need to install the SQL Server desktop engine included with Visual Studio.NET. Note that if you select the desktop engine as part of the Visual Studio installation, the engine will not install. You still need to run its installation file. This file (Setup.exe) is located in the Setup/MSDE directory in the Visual Studio.NET installation folder. This installs a SQL Server with the instance name YOURMACHINEVSDOTNET. Reboot after installing.

Next, install the Northwind and Pubs databases by opening a DOS box and running the following osql commands:

osql E S COMPUTERNAMEVSDOTNET i "C:PROGRAM FILESMICROSOFT VISUALSTUDIO .NETFRAMEWORKSDKSAMPLESSETUPinstpubs.sql"

and

osql E S COMPUTERNAMEVSDOTNET i "C:PROGRAM FILESMICROSOFT VISUALSTUDIO .NETFRAMEWORKSDKSAMPLESSETUPinstnwnd.sql"

Replace COMPUTERNAME with the name of your computer, and keep the quotes. Note that there is a space between the ‘Studio’ and the ‘.NET’.

The SQL Server service manager will start by default, however it will not start the SQL Service itself by default. To configure the SQL Server Service Manager to automatically start when Windows starts up, open the service manager from your taskbar or from the startup folder within the start menu. Select ‘SQL Server’ from the ‘Services’ drop-down list and check the ‘Auto-start service when OS starts’ checkbox.

Reboot and you are ready to build a Web service that exposes this data.

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 COMPUTERNAMEVSDOTNET. 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:
    http://localhost/nwind/service1.asmx/GetProductsData?  http://localhost/dfconfig2.xml  //ProductsDataSet/Products[UnitPrice>40]/./UnitsInStock//ProductsDataSet/Products[UnitPrice>40]/./ProductName  600  400  http://localhost/Nwind/Service1.asmx?WSDL  http://localhost/dfconfig2.xml  //UnitPrice   //ProductName  600  400  http://tempuri.org/GetProductsData  GetProductsData

    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.
    • The SOAP Load Function
      A new function has been added to the chart server since the previous article’s publication. This function uses the parameters defined in the charts.xml file above to discover and call the Web service using SOAP.

      It needs to be as generic as possible so you can pass in the specifics for its location, namespace, and required functions. As such, the SOAP load function takes the following parameters:

      strLocation

      Specifies the WSDL file containing the description of the web service.

      strSoapAction

      Specifies the namespace and method for the action that we will be performing.

      strSoapFunction

      Specifies the fully qualified SOAP Method that will be used within the SOAP envelope.

      strWrapperNameSpace

      Specifies the namespace to use.

      Table 1: Parameters that are passed to the SOAP Load function and their description.

      First, the function creates a Http connection object and points it at the WSDL for the Web service, which was automatically generated by Visual Studio.NET when you built the Web service. When consuming any Web service, you need to view the WSDL. To view the WSDL when a Web service is generated using Microsoft Visual Studio.Net, run the Web service, and get the standard Microsoft service front-end. At the top of the screen, there is a hyperlink to the ‘Service Description’. This takes you to the WSDL for your Web service.

      Note: When configuring a chart to consume a Web service, the setting must point to the WSDL file for the Web service. This allows the charter to automatically generate soap messages of the required structure.

      In addition the function needs to specify the SOAP Action, which is the method that defines the function that you are going to call. In the WSDL file, find a node called:

      	Within this node, there's a childnode called :	 

      ‘GetProductsData’ is the name of a public function defined within your Web service. Within this childnode, the SOAP action is specified. If you use the default settings when creating the Nwind Web service (above), the SOAP action for this function is defined as ‘http://tempuri.org/GetProductsData’. The connector object uses this value in its ‘SoapAction’ property. You configure this value in the the node in the charts.xml file.

      The following code shows how these properties are used to specify the SOAP connector to the desired Web service using VB.NET.

              Dim connector As New MSSOAPLib30.HttpConnector30Class()        connector.Property("EndPointURL") = strLocation        connector.Property("SoapAction") = strSoapAction

      Next you set up a SOAP Serializer object, which creates a SOAP command envelope. In this case, you’re calling a simple function on the Web service which takes no parameters. The chart server is easily expanded to tokenize parameters within the charts.xml file and pass them into this function. The serializer object allows you to add these parameters as elements to pass to the Web service.

       
      Figure 1: This is a sample of the data charted from the Northwind data source.

      The most important part of the SOAP Load function is the ‘StartElement’ property setting. This setting passes the other configurable parameters. If you go back to the WSDL file again, and look at the binding node for the SOAP messages on this service, you will see that the node containing the soap action was called ‘operation’.

      The contents of this node should be set in the charts.xml file in the node. In addition, you need to specify the the wrapper namespace. Use the ‘targetNameSpace’ value specified in the WSDL with the name of the Web service and the message type appended. For example, you are using a SOAP message, so the wrapper name space would be ‘http://tempuri.org/nwind/message’. This code shows how to create a serializer object that contains the SOAP message:

              Dim serializer As New MSSOAPLib30.SoapSerializer30Class()        serializer.Init(connector.InputStream)        serializer.StartEnvelope("", "", "")        serializer.StartBody("STANDARD")        serializer.StartElement(strSoapFunction, strWRAPPERNAMESPACE, "", "")        serializer.EndElement()        serializer.EndBody()        serializer.EndEnvelope()        serializer.Finished()

      Next, the function reads the response from the service using a SOAP reader class, and loads the contents of the response into an XML Document:

              Dim reader As New MSSOAPLib30.SoapReader30Class()        reader.Load(connector.OutputStream, "")        Dim strTest As String        strTest = reader.BodyEntries(0).xml        Dim xDoc As New System.Xml.XmlDocument()        xDoc.LoadXml(strTest)        Return xDoc

      Expanded Functionalities
      Figure 1 shows the result of charting the Products table from the Northwind database, with the data exposed as a Web service, and an XPath variable specifying the UnitsInStock for all items whose unit price is greater than $40.

      This shows the power and extensibility of the charting server. Given a URL or a WSDL file, the charter will consume the data and produce a chart according to your requirements.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist