Home > Articles > Web Services > XML

  • Print
  • + Share This
This chapter is from the book

SQLXML's Web service support allows you to expose SQL Server as a Web service. This allows stored procedures, other procedural objects, and query templates to be executed as though they were methods exposed by a traditional SOAP-based Web service. SQLXML provides the plumbing necessary to access SQL Server data using SOAP from any platform or client that can make SOAP requests.

The advantage of this, of course, is that you don't need SQL Server client software to run queries and access SQL Server objects. This means that applications on client platforms not directly supported by SQL Server (e.g., Linux) can submit queries and retrieve results from SQL Server via SQLXML and its SOAP facility.

You set up SQL Server to masquerade as a Web service by configuring a SOAP virtual name in the IIS Virtual Directory Management tool. (You can find this under the SQLXML | Configure IIS menu option under Start | Programs.) A SOAP virtual name is simply a folder associated with an IIS virtual directory name whose type has been set to soap. You can specify whatever service name you like in the Web Service Name text box; the conventional name is soap. Once this virtual name is set up, you configure specific SQL Server objects to be exposed by the Web service by clicking the Configure button on the Virtual Names tab and selecting the object name, the format of the XML to produce on the middle tier (via SQLISAPI), and the manner in which to expose the object: as a collection of XML elements, as a single Dataset object, or as a collection of Datasets. As the exercise we'll go through in just a moment illustrates, you can expose a given server object multiple times and in multiple ways, providing client applications with a wealth of ways to communicate with SQL Server over SOAP.

Architecturally, SQLXML's SOAP capabilities are provided by its ISAPI extension, SQLISAPI. These capabilities are an extension of the virtual directory concept that you configure in order to access the server via URL queries and templates. The SOAP virtual name that you set up provides access to SQLXML's Web service facility via a URL. It allows any client application that can communicate over SOAP with this URL to access SQL Server objects just as it would any other Web service. Java applications, traditional ADO applications, and, of course, .NET applications can access SQL Server procedural objects and XML templates without using traditional SQL Server client software or communicating over TDS.

In this next exercise, we'll walk through exposing SQL Server as a Web service and then consuming that service in a C# application. We'll set up the SOAP virtual name, then we'll configure a SQL Server procedure object to be exposed as a collection of Web service methods. Finally, we'll build a small application to consume the service and demonstrate how to interact with it.

Exercise 18.4 Building and Consuming a SQLXML Web Service
  1. Under the \inetpub\wwwroot\Northwind folder that you created earlier, create a folder named Soap.

  2. Start the IIS Virtual Directory Management for SQLXML tool that you used to configure the Northwind virtual folder earlier.

  3. Go to the Virtual Names tab and add a new virtual name with a Name, Type, and Web Service Name of soap. Set the path to the folder you created in step 1.

  4. Save the virtual name configuration. At this point, the Configure button should be enabled. Click it to begin exposing specific procedural objects and templates via the Web service.

  5. Click the ellipsis button to the right of the SP/Template text box and select the ListCustomers stored procedure from the list.

  6. Name the method ListCustomers and set its row format to Raw and its output format to XML objects, then click OK.

  7. Repeat the process and name the new method ListCustomersAsDataset (you will be referencing the ListCustomers stored procedure). Set its output type to Single dataset, then click OK.

  8. Repeat the process again and name the new method ListCustomersAsDatasets. Set its output type to Dataset objects, then click OK. You've just exposed the ListCustomers stored procedure as three different Web service methods using three different output formats. Note that procedural objects you set up this way must not return XML themselves (i.e., they must not use the Transact-SQL FOR XML option) because XML formatting is handled exclusively at the middle tier by SQLISAPI when using the SQLXML Web service facility.

  9. Start a new C# Windows application project in Visual Studio .NET. The app we'll build will allow you to invoke the SQLXML Web service facility to execute the ListCustomers stored proc using a specified CustomerID mask.

  10. Add a single TextBox control to the upper-left corner of the default form to serve as the entry box for the CustomerID mask.

  11. Add a Button control to the right of the TextBox control to be used to execute the Web service method.

  12. Add three RadioButton controls to the right of the button to specify which Web method we want to execute. Name the first rbXMLElements, the second rbDataset, and the third rbDatasetObjects. Set the Text property of each control to a brief description of its corresponding Web method (e.g., the Text property for rbXMLElements should be something like “XML Elements”).

  13. Add a ListBox control below the other controls on the form. This will be used to display the output from the Web service methods we call. Dock the ListBox control to the bottom of the form and be sure it is sized to occupy most of the form.

  14. Make sure your instance of IIS is running and accessible. As with the other Web-oriented examples in this chapter, I'm assuming that you have your own instance of IIS and that it's running on the local machine.

  15. Right-click your solution in the Solution Explorer and select Add Web Reference. In the URL for the Web reference, type the following:

    http://localhost/Northwind/soap?wsdl

    This URL refers by name to the virtual directory you created earlier, then to the soap virtual name you created under it, and finally to the Web Services Description Language (WSDL) functionality provided by SQLISAPI. As I mentioned earlier, a question mark in a URL denotes the start of the URL's parameters, so wsdl is being passed as a parameter into the SQLISAPI extension DLL. Like XML and SOAP, WSDL is its own W3C standard and describes, in XML, Web services as a set of end points operating on messages containing either procedural or document-oriented information. You can learn more about WSDL by visiting this link on the W3C Web site: http://www.w3.org/TR/wsdl.

  16. Once you've added the Web reference, the localhost Web service will be available for use within your application. A proxy class is created under your application folder that knows how to communicate with the Web service you referenced. To your code, this proxy class looks identical to the actual Web service. When you make calls to this class, they are transparently marshaled to the Web service itself, which might reside on some other machine located elsewhere on the local intranet or on the public Internet. You'll recall from Chapter 6 that I described Windows' RPC facility as working the very same way. Web services are really just an extension of this concept. You work and interoperate with local classes and methods; the plumbing behind the scenes handles getting data to and from the actual implementation of the service without your app even being aware of the fact that it is dealing with any sort of remote resource.

  17. Double-click the Button control you added earlier and add to it the code in Listing 18.87.

    Listing 18.87

    int iReturn = 0;
    object result;
    object[] results;
    System.Xml.XmlElement resultElement;
    System.Data.DataSet resultDS;
    localhost.soap proxy = new localhost.soap();
    proxy.Credentials=System.Net.CredentialCache.DefaultCredentials;
    
    // Return ListCustomers as XMLElements
    if (rbXMLElements.Checked)
    {
      listBox1.Items.Add("Executing ListCustomers...");
      listBox1.Items.Add("");
    
      results = proxy.ListCustomers(textBox1.Text);
    
      for (int j=0; j<results.Length; j++)
      {
        localhost.SqlMessage errorMessage;
        result= results[j];
    
        if (result.GetType().IsPrimitive)
        {
          listBox1.Items.Add(
            string.Format("ListCustomers return value: {0}", result));
        }
        if (result is System.Xml.XmlElement)
        {
          resultElement = (System.Xml.XmlElement) results[j];
          listBox1.Items.Add(resultElement.OuterXml);
        }
        else if (result is localhost.SqlMessage) {
          errorMessage = (localhost.SqlMessage) results[j];
          listBox1.Items.Add(errorMessage.Message);
          listBox1.Items.Add(errorMessage.Source);
        }
      }
      listBox1.Items.Add("");
    }
    // Return ListCustomers as Dataset objects
    else if (rbDatasetObjects.Checked)
    {
      listBox1.Items.Add("Executing ListCustomersAsDatasets...");
      listBox1.Items.Add("");
      results = proxy.ListCustomersAsDatasets(textBox1.Text);
    
      for (int j=0; j<results.Length; j++)
      {
        localhost.SqlMessage errorMessage;
        result= results[j];
    
        if (result.GetType().IsPrimitive)
        {
          listBox1.Items.Add(
            string.Format("ListCustomers return value: {0}", result));
        }
        if (result is System.Data.DataSet)
        {
          resultDS = (System.Data.DataSet) results[j];
          listBox1.Items.Add("DataSet " +resultDS.GetXml());
        }
        else if (result is localhost.SqlMessage)
        {
          errorMessage = (localhost.SqlMessage) results[j];
          listBox1.Items.Add("Message " +errorMessage.Message);
          listBox1.Items.Add(errorMessage.Source);
        }
      }
      listBox1.Items.Add("");
    }
    // Return ListCustomers as Dataset
    else if (rbDataset.Checked)
    
    {
      listBox1.Items.Add("Executing ListCustomersAsDataset...");
      listBox1.Items.Add("");
      resultDS = proxy.ListCustomersAsDataset(textBox1.Text,
          out iReturn);
      listBox1.Items.Add(resultDS.GetXml());
      listBox1.Items.Add(
        string.Format("ListCustomers return value: {0}", iReturn));
      listBox1.Items.Add("");
    }
    
  18. This code can be divided into three major routines—one each for the three Web service methods we call. Study the code for each type of output format and compare and contrast their similarities and differences. Note the use of reflection in the code to determine what type of object we receive back from Web service calls in situations where multiple types are possible.

  19. Compile and run the app. Try all three output formats and try different CustomerID masks. Each time you click your Button control, the following things happen.

    1. Your code makes a method call to a proxy class Visual Studio .NET added to your project when you added the Web reference to the SQLXML SOAP Web service you set up for Northwind.

    2. The .NET Web service code translates your method call into a SOAP call and passes it across the network to the specified host. In this case, your Web service host probably resides on the same machine, but the architecture allows it to reside anywhere on the local intranet or public Internet.

    3. The SQLXML ISAPI extension receives your SOAP call and translates it into a call to the ListCustomers stored procedure in the database referenced by your IIS virtual directory, Northwind.

    4. SQL Server runs the procedure and returns its results as a rowset to SQLISAPI.

    5. SQLISAPI translates the rowset to the appropriate XML format and object based on the way the Web service method you called was configured, then returns it via SOAP to the .NET Framework Web service code running on your client machine.

    6. The .NET Framework Web services code translates the SOAP it receives into the appropriate objects and result codes and returns them to your application.

    7. Your app then uses additional method calls to extract the returned information as text and writes that text to the ListBox control.

So, there you have it, a basic runthrough of how to use SQLXML's SOAP facilities to access SQL Server via SOAP. As I've said, an obvious application of this technology is to permit SQL Server to play in the Web service space—to interoperate with other Web services without requiring the installation of proprietary client software or the use of supported operating systems. Thanks to SQLXML's Web service facility, anyone who can speak SOAP can access SQL Server. SQLXML's Web service support is a welcome and very powerful addition to the SQL Server technology family.

  • + Share This
  • 🔖 Save To Your Account