ONDotNet.com    
 Published on ONDotNet.com (http://www.ondotnet.com/)
 See this if you're having trouble printing code examples


A Quick Tour of .NET Web Services

by Brian Jepson
02/25/2002

Here are some simple .NET Web services examples from a recent talk I gave.

"Hello, World" Web Service

This is a simple Web service that returns the string "Hello, World." You should save it as HelloWorld.asmx in a virtual directory (that is configured to execute scripts):

Example 1. Hello, World Web Service


<%@ WebService Language="C#" Class="OReilly.HelloWorld" %>
namespace OReilly 
{
  using System;
  using System.Web.Services;
  [WebService(Namespace="http://www.oreilly.com/webservices/")]
  public class HelloWorld : WebService
  {
    [WebMethod(Description="Says hello.")]
    public string Hello()
    {
      return "Hello, World.";
    }
  }
}

Console Client

This application acts as a client to the Web service. Before you can compile it, use wsdl.exe to create a proxy class with the command wsdl <url-of-web-service>?WSDL (for example, wsdl http://localhost/Services/HelloWorld.asmx?WSDL). This will create the proxy program HelloWorld.cs. You can then compile HelloClient.cs against it with these commands:


csc /t:library HelloWorld.cs
csc HelloClient.cs /r:HelloWorld.dll

Run the resulting executable to invoke the "Hello, World" Web service. Here is the source code for HelloClient.cs:

Example 2. "Hello, World" Client Console


// HelloClient.cs
//
using System;
public class HelloClient
{
  public static void Main()
  {
    HelloWorld objWS = new HelloWorld();
    Console.WriteLine(objWS.Hello());
  }
}

ADO.NET Web Service

Here is an ADO.NET Web service that returns a DataSet as its result; save it as ADOWebSvc.asmx in a virtual directory that has script-execute permissions. Before you can use it, you must install the .NET QuickStart Samples. You can install examples from Start> Microsoft .NET Framework SDK > Samples and QuickStart Tutorials. Follow the instructions to install MSDE and the sample data.

Example 3. ADO.NET Web Service


<%@ WebService Language="C#" Class="ADOWebSvc" %>

// ADO.NET Web Service Sample
// based on an example from Chapter 6 of
// .NET Framework Essentials
//    by Thuan Thai and Hoang Q. Lam
//    (2002, O'Reilly & Associates)

using System;
using System.Data;
using System.Data.OleDb;
using System.Web;
using System.Web.Services;

[WebService(Namespace="http://www.oreilly.com/webservices")]
public class ADOWebSvc : WebService
{
  string cs = "provider=sqloledb;server=(local)\\NetSDK;" +
    "Integrated Security=SSPI;database=pubs;uid=sa;pwd=;";

  [WebMethod(Description="Fetch all authors.")]
  public DataSet GetAuthors()
  {
    OleDbDataAdapter adpt = 
      new OleDbDataAdapter("SELECT * FROM authors", cs);
    DataSet ds = new DataSet();
    adpt.Fill(ds);
    return ds;
  }
}

Web Form Client

Comment on this articleWhat do you think of these examples to get you started, especially with Visual Foxpro?
Post your comments

This example will contact the ADO.NET Web service and generate an HTML table that is filled with the result set's data.

This file should be saved as an ASP.NET file, such as ADOClient.aspx. You can save it on the same server that hosts the Web service, or on a different server. As with the "Hello, World" example, you need to create a proxy using wsdl.exe (for example, wsdl http://localhost/Services/ADOWebSvc.asmx?WSDL). You don't need to compile it into a DLL. Instead, make sure the client (ADOWebSvc.cs) is stored in the same directory as the .aspx file. Figure 1 shows how this Web page will appear in the browser.


Figure 1. Browsing the a .NET data set from a Web Forms client.

Example 4. ADO.NET Web Service Client


<!--
     ADO.NET Web Service Client
     based on an example from Chapter 6 of
     .NET Framework Essentials
        by Thuan Thai and Hoang Q. Lam
        (2002, O'Reilly & Associates)
-->

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>

<!-- link to the proxy generated by wsdl.exe -->
<%@ Assembly Src="ADOWebSvc.cs" %>

<html>
  <body>
  <head>
  <title>SOAP Client</title>
  </head>
  
    <!-- Make the SOAP call and fill the data grid -->
    <%
      Dim ws as ADOWebSvc = new ADOWebSvc()
      Dim ds as DataSet = ws.GetAuthors()
      dg.DataSource = ds.Tables(0).DefaultView
      dg.DataBind()
    %>

    <!-- Create a DataGrid.  Note: this code must appear 
         after the call to DataBind() 
      -->
    <asp:DataGrid id="dg" runat="server"/>

  </body>
</html>

Windows Forms Client

Here is a Windows Forms client that you can link against the ADOWebSvc.cs proxy that was generated by wsdl.exe. Save this as WFClient.cs, and compile it with:


  csc /t:library ADOWebSvc.cs
  csc WFClient.cs /r:ADOWebSvc.dll

Example 5. Source code for WFClient.cs


// ADO.NET Web Service Client
// based on an example from Chapter 6 of
// .NET Framework Essentials
//    by Thuan Thai and Hoang Q. Lam
//    (2002, O'Reilly & Associates)

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;

class WinFormClient
{
  static void Main() 
  {
    // Create the proxy.
    ADOWebSvc proxy = new ADOWebSvc();

    // Get the data set.
    DataSet ds = proxy.GetAuthors();

    // Associate a data grid with 
    // the DataSet.
    DataGrid dg = new DataGrid();
    dg.Size = new Size(590, 370);
    dg.DataSource = 
      ds.Tables[0].DefaultView;

    // Create a form and add the 
    // DataGrid to it.
    Form f = new Form();
    f.Text = "DataSet/DataGrid";
    f.Size = new Size(600, 400);
    f.Controls.Add(dg);

    // Start the application.
    Application.Run(f);
  }
}

Figure 2 shows this application in action.


Figure 2. Browsing the a .NET data set from a Windows Forms client.

Visual FoxPro Client

Visual FoxPro wasn't as easy, but you can pull the XML-serialized data set into a text file and start mercilessly chopping away at it until something feeds into Visual FoxPro's XMLTOCURSOR() function.

When I get the data set back from the .NET Web service, it looks roughly like this:


<DataSet> ... 
  <xs:schema> ... </xs:schema>
  <diffgr:diffgram> ...
    <NewDataSet> ... </NewDataSet>
  </diffgr:diffgram>
</DataSet>

You want the following <NewDataSet> element and everything it contains. The only problem is that some of the elements in it contain references to outer namespaces, as in:


<Authors diffgr:id="Authors2" msdata:rowOrder="1">

By stripping these out, you end up with something to feed into XMLTOCURSOR() and browse (see Figure 3).


Figure 3. Browsing the a .NET data set.

Example 6. Visual FoxPro Client


LOCAL oWS AS ADOTest
********************************************
* IntelliSense-generated code appears here *
********************************************

LOCAL dotnetDS as String
LOCAL vfpDS as String
LOCAL dsrows as String

* Get the .NET Data set. It looks like:
*   <DataSet> ... 
*     <xs:schema> ... </xs:schema>
*     <diffgr:diffgram> ...
*       <NewDataSet> ... </NewDataSet>
*     </diffgr:diffgram>
*   </DataSet>
*
* We want the NewDataSet, since it contains the rows.
*
dotnetDS = oWS.GetAuthors
dsRows = dotnetDS.item[1].firstChild.xml

* strip out references to XML namespaces that are
* no longer part of this XML document.
vfpDS = STRTRAN(STRTRAN(dsRows, "diffgr:", ""), "msdata:", "")

* Perform the conversion.
XMLTOCURSOR(vfpDS)

* See how it looks!
BROWSE


Not quite XSLT, but it gets the job done quickly and easily. For documentation on registering Web services with Visual FoxPro and using IntelliSense to insert consumer code, see this MSDN article.

Brian Jepson is an O'Reilly editor, programmer, and co-author of Mac OS X Panther for Unix Geeks and Learning Unix for Mac OS X Panther. He's also a volunteer system administrator and all-around geek for AS220, a non-profit arts center in Providence, Rhode Island. AS220 gives Rhode Island artists uncensored and unjuried forums for their work. These forums include galleries, performance space, and publications. Brian sees to it that technology, especially free software, supports that mission. You can follow Brian's blog here.


Return to the .NET DevCenter.

Copyright © 2009 O'Reilly Media, Inc.