Tutorial: Accessing web services from Microsoft .NET

The following tutorial demonstrates how to access web services from Microsoft .NET using Visual C#.

To create SOAP and DISH services
  1. At a command prompt, run the following command to start a personal web server. Replace samples-dir with the actual location of the sample database. The -xs http(port=80) option tells the database server to accept HTTP requests on port 80. If you already have a web server running on port 80, use another port number such as 8080 for this tutorial and use 8080 instead of 80 in all port references.

    dbeng11 -xs http(port=80) samples-dir\demo.db
  2. Start Interactive SQL. Connect to the SQL Anywhere sample database as the DBA. Execute the following statements:

    1. Define a SOAP service that lists the Employees table.

      CREATE SERVICE "SASoapTest/EmployeeList"
      TYPE 'SOAP'
      AUTHORIZATION OFF
      SECURE OFF
      USER DBA
      AS SELECT * FROM Employees;

      Because authorization has been turned off, anyone can use this service without supplying a user name and password. The commands run under user DBA. This arrangement is simple, but insecure.

    2. Create a DISH service to act as a proxy for the SOAP service and to generate the WSDL document.

      CREATE SERVICE "SASoapTest_DNET"
      TYPE 'DISH'
      GROUP "SASoapTest"
      FORMAT 'DNET'
      AUTHORIZATION OFF
      SECURE OFF
      USER DBA;

      The SOAP and DISH service must be of format DNET. In this example, the FORMAT clause was omitted when the SOAP service was created. As a result, the SOAP service inherits the DNET format from the DISH service.

  3. Start Microsoft Visual Studio. Note that this example uses functions from the .NET Framework 2.0.

    1. Create a new Windows application project using Visual C#.

      An empty form appears.

    2. From the Project menu, choose Add Web Reference.

    3. In the URL field of the Add Web Reference page, enter the following URL: http://localhost:80/demo/SASoapTest_DNET.

    4. Click Go.

      You are presented with a list of the methods available for SASoapTest_DNET. You should see the EmployeeList method.

    5. Click Add Reference to finish.

      The Solution Explorer window shows the new web reference.

    6. From the Visual Studio Toolbox, add a listbox and a button to the form as shown in the following diagram.

      SOAP demonstration form.
    7. Rename the button text to Employee List.

    8. Double-click Employee List and add the following code for the button click event.

      int sqlCode;
      
      listBox1.Items.Clear();
      
      localhost.SASoapTest_DNET proxy = new localhost.SASoapTest_DNET();
      
      DataSet results = proxy.EmployeeList(out sqlCode);
      DataTableReader dr = results.CreateDataReader();
      while (dr.Read())
      {
        for (int i = 0; i < dr.FieldCount; i++)
        {
          string columnName = dr.GetName(i);
          try
          {
            string value = dr.GetString(i);
            listBox1.Items.Add(columnName + "=" + value);
          }
          catch ( InvalidCastException )
          {
            listBox1.Items.Add(columnName + "=(null)");
          }
        }
        listBox1.Items.Add("");
      }
      dr.Close();
    9. Build and run the program.

      The listbox will display the EmployeeList result set as column name=value pairs.

      A try/catch block is included to handle NULL column values such as those found in the TerminationDate column of the Employees table.