Consuming a Web Service in C#


This tutorial teaches you how to “consume” a simple web service, Circle24_WorldTime, that allows you to find the time in major cities around the world using C#.

This tutorial is for beginners to the C# language, .NET and XML web services and therefore will not go into great detail nor explain in depth how XML web services work. This also assumes that you do not have Visual Studio.NET.


What you need:

.NET SDK (get it here).

A text editor (I am using Notepad).

In this tutorial we will be creating a program that asks the user for the name of a city and then gets the time and date for that city. Here is the output of the completed program:

Enter City: New York
07:01 PM Friday, Jul 12 2002
Press any key to continue . . .

We will be using a web service called Circle24_WorldTime. The web service has one method called GetTime(string City) of which you pass a string containing the name of a city. If will return the current date and time of that city.

The fist step is to create a web service proxy. The proxy acts as a “mediator” between your program and the web service. The proxy is create using a tool called wsdl.exe. This program generates C# code for the WSDL of the web service. First you have to understand what WSDL is.
Here is the official (W3C) explanation:

“WSDL is an XML format for describing network services as a set of endpoints operating on messages containing either document-oriented or procedure-oriented information. The operations and messages are described abstractly, and then bound to a concrete network protocol and message format to define an endpoint. Related concrete endpoints are combined into abstract endpoints (services). -W3C

Basically, the WSDL describes the methods that the web service supports and how to access them, hence its name Web Service Description Language

The address of the WSDL for Circle24_WorldTime is located at

To generate the code for the proxy type this into the command line:


if done correctly this should be displayed:

Now you need to compile it. Type this into the command line:

csc /t:library WorldTime.cs.

This will create a DLL called WorldTime.dll.

Now we will start programming. Create a file called Time.cs and add the following code.
NOTE: Time.cs MUST be in the same directory as WorldTime.dll.

The code:

<nobr />

<nobr>using System;

class Time
    public static void Main(string[] args)

        //Create instace of the WorldTime.dll
        WorldTime Time= new WorldTime();

        //get input from user
        Console.Write("Enter City: ");
        string City=Console.ReadLine();

        //gets date and time
        string DateTime=Time.GetTime(City);

        //write the citys time and date


<nobr />

Code was coloured using

You must now compile Time.cs. In the command time type:

csc /r:WorldTime.dll Time.cs

My output was as follows:

C:\Programming\csharp\WorldTime>csc /r:WorldTime.dll Time.cs
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. All rights reserved.

This will generate Time.exe.

The result from running Time.exe on my computer

Enter City: Auckland
02:20 PM Saturday, Jul 13 2002

Well done, you have consumed your first web service. Now you can integrate the many free web services that are avalible on the net within you apps.

ack">I hope this tutorial was informative and easy. Please email me with comments, corrections or help relating to this article or ideas for other articles.

Written by Paul William


Circle24_WorldTime documentation:


Thanks to Cander for help.

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!