AddThis Social Bookmark Button

Print

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>

Pages: 1, 2

Next Pagearrow