Click here to Skip to main content
11,922,641 members (57,414 online)
Click here to Skip to main content
Add your own
alternative version


49 bookmarked

How to make a simple WebService and consume it.

, 11 Sep 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
How to make a simple WebService and consume it.


We all talk about webservices, webservices can do this and webservices can do that. But when we are asked to make one, we hesitate. Maybe it's because we never made a webservice before, and all the time playing with Webforms and Windows Forms or even Console Applications. By the way, I love Console applications. In this article, I will show you how to create a simple webservice that is consumed by a Console application client.

Making the WebService:

First, start your Visual Studio .NET, and in the project type, select ASP.NET WebService. In the left pane, choose the language of your choice. In this example, I will be using Visual C#.NET. Once you select the project, a page will appear which will be more like a design page, switch to its code view. In the code view, you can see lot of comments and C# code already written for you. You will also see that at the bottom, there is a method HelloWorld which is written for you by default, so you can test your service and of course say hello to the world. After removing the unwanted code and comments, your code will look like this:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace WebServiceExample
    public class Service1 : System.Web.Services.WebService
        public Service1()

        // Add the Description Attribute so you
        // will know the purpose of your WebService
        [WebMethod(Description="This Method prints HelloWorld")]
        public string HelloWorld()
            return "Hello World";

Let's dig into this small code. [WebMethod] Attribute denotes that this method will be used by the clients, also this method has to be public in order for a client to use it. Description inside the WebMethod Attribute just gives the method more meaning. Don't worry about the InitializeComponent() method since it's written by default.

Running the WebService:

OK, now you have made your first kick ass WebService (without even writing a single line of code). Let's run it and check whether it gives the correct result or not. In the Solution Explorer, right click on the .asmx file, and select View in Browser as shown below:

Sample screenshot

Once you click on "View in Browser", the next screen you will see will be something like this:

Sample screenshot

I have erased most of the stuff, so you can only see the method that you need in this example. Below, you can see the method HelloWorld and the description you wrote for the method.

Now, click on the HelloWorld method. I don't want to scare you with all the SOAP and HTTP code produced, so I am only going to paste the screen shot which will be relevant to this example.

Sample screenshot

Alright, so far so good. Now, just press the Invoke button to see the result of your method named HelloWorld().

Sample screenshot

This is cool. You have just tested your first webservice and it ran since you didn't coded it. I know what you are thinking right now. Is the client going to see the result like this strange format (this is XML format). Well, of course not. That's why you need to make a Proxy class which consumes this service.

Making a Proxy Client:

Let's make a Console application which consumes this service. You can use any language and platform to consume this service, that's the purpose of XML WebService. Now, this procedure requires some mouse clicking Smile | :) . So I will write down the steps instead of pasting the screen shots.

  1. Start a new project which will be a Console Application in Visual C#.NET.
  2. Once you see the code view in the Console application, right click on the project name from the Solution Explorer. Remember that project name will be written in bold.
  3. Click on "Add Web Reference".
  4. Paste the URL of your WebService. You can get the URL of your WebService when you view your webservice in IE or any other browser.
  5. Click GO.
  6. Your webservice will be loaded. In the Web Reference Name textbox, write "MyService" and click Add Reference.
  7. You will see that the web reference has been added in your Solution Explorer, meaning that webservice is ready to kick some butt.

Now, all you have to do is to make the instance of the WebService class using the reference name that you provided, which is "MyService".

using System;

namespace MyClient
    class Class1
        static void Main(string[] args)
            // Make an instance of the WebService Class
            // using the Web Reference you provided
            MyService.Service1 service = new MyService.Service1(); 
            // Assign message what ever is returned
            // from HelloWorld in this case "HelloWorld"
            string message = service.HelloWorld(); 
            // Prints out the message on the screen


And that's it. You use the webservice class just like any other class. Something you need to keep in mind is that if you decide to make a new method in your webservice and want to make it available to the client, then always remember to build your webservice solution so that the assembly can be updated. If you don't build your webservice, you won't be able to see the methods on the client side.

I guess that's it!!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United States United States
I am the founder of knowledge base website, HighOnCoding, GridViewGuy, and

HighOnCoding is a website which will get you high legally with useful information. There are tons of articles, videos and podcasts hosted on HighOnCoding.

My Blog:


Buy my iPhone app ABC Pop

You may also be interested in...

Comments and Discussions

QuestionHow to use this service from Online Pin
ktlinn12-Apr-15 15:14
memberktlinn12-Apr-15 15:14 
QuestionWeb Service in HTML Pin
Member 1114999713-Oct-14 7:25
memberMember 1114999713-Oct-14 7:25 
GeneralMy vote of 1 Pin
Lars Bernstorff Hansen17-Sep-14 4:29
memberLars Bernstorff Hansen17-Sep-14 4:29 
GeneralMy vote of 5 Pin
SChristmas8-Jun-12 8:04
memberSChristmas8-Jun-12 8:04 
QuestionInner Exception {"The remote server returned an error: NotFound."} Pin
darkhorse_code4-Apr-11 18:07
memberdarkhorse_code4-Apr-11 18:07 
GeneralMy vote of 2 Pin
kingng2-Nov-10 3:39
memberkingng2-Nov-10 3:39 
GeneralPlease solve my problem Pin
Blue stone21-Dec-08 3:39
memberBlue stone21-Dec-08 3:39 
General[Message Removed] Pin
hankjmatt13-Oct-08 22:42
memberhankjmatt13-Oct-08 22:42 
GeneralHelp Please Pin
DR Delphi13-Oct-08 15:33
memberDR Delphi13-Oct-08 15:33 
GeneralGood,But it is too simple. Pin
careast16-Mar-08 21:34
membercareast16-Mar-08 21:34 
GeneralGood Introduction Pin
Jitku1-Nov-07 13:47
memberJitku1-Nov-07 13:47 
GeneralRe: Good Introduction Pin
Member 14713651-Apr-10 21:26
memberMember 14713651-Apr-10 21:26 
GeneralMisleading info Pin
Pateprad22-Jan-07 0:20
memberPateprad22-Jan-07 0:20 
GeneralRe: Misleading info Pin
azamsharp22-Jan-07 14:06
memberazamsharp22-Jan-07 14:06 
QuestionRe: Misleading info Pin
adityareddyp9-Oct-07 8:45
memberadityareddyp9-Oct-07 8:45 
QuestionHow to Use Webservice tranfer pass Proxy? Pin
khoadd21-Jul-06 19:38
memberkhoadd21-Jul-06 19:38 
Questionstandalone soap service Pin
frohwein20-Jul-06 5:18
memberfrohwein20-Jul-06 5:18 
GeneralError Handling Pin
skrishnasarma26-May-05 6:18
memberskrishnasarma26-May-05 6:18 
GeneralGood introduction Pin
Steven Campbell12-Sep-04 6:57
memberSteven Campbell12-Sep-04 6:57 
GeneralRe: Good introduction Pin
azamsharp12-Sep-04 8:02
memberazamsharp12-Sep-04 8:02 
GeneralRe: Good introduction Pin
Willis King13-Sep-04 9:19
sussWillis King13-Sep-04 9:19 
GeneralRe: Good introduction Pin
dinaayoub14-Mar-05 15:08
memberdinaayoub14-Mar-05 15:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151125.1 | Last Updated 12 Sep 2004
Article Copyright 2004 by azamsharp
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid