Home   Cover Cover Cover Cover

A Simple Web Service (TimeService)

From Section 7.2 of the book

This example shows how to write a simple web service with a method GetTime that reads the current time on the server and sends it to the client.

<%@ WebService Language="C#" Class="TimeService" %>
using System.Web.Services;

public class TimeService : WebService {
  [WebMethod(Description="Returns the current time")]
  public string GetTime() { 
    return System.DateTime.Now.ToLongTimeString(); 

If you copy this code to a file TimeService.asmx in a virtual directory of your local machine you can open it with Internet Explorer and get a test page that allows you to invoke the method GetTime.

Alternatively, you can test your web service also with WebService Studio.