Main menu

In one of my previous articles, I have explained briefly about what the web service is and how we can create a web service in ASP.NET. When a web service is created, the next step is to learn how we can use this web service. Well, sometimes we just need to learn how to consume web services created by others. That is the whole purpose of the web service. They are created to be used by other applications. So this article will explain how we can use a web service.

The first step to use a web service is to add the reference of that web service in your application. You need to copy the URL of the web service which you want to use and follow below steps. For this article, I am using web service which I have created in my previous article.

Select Add Service Reference from Web Site

Then Select Advanced and then Add Web Reference

Enter URL, change the name of web service and click Add Reference

Now you need to use web service namespace in your code file. The namespace will be the name of your web service.

1

using MyWebService;

Now you can call web methods of web service in your code by creating an instance of that web service. It is same like you use any class in your code. You need to create the instance of that class and then call the methods of that class. I have called both web methods of my web service.

1

2

3

4

5

6

7

8

9

10

protectedvoidPage_Load(objectsender,EventArgse)

{

WebService aSerice=newWebService();

stringaString=aSerice.HelloWorld();

intresult=aSerice.Multiply(5,5);

Response.Write(aString+"<br/>");

Response.Write("Result is: "+result);

Response.End();

}

You have to remember few things when creating new web methods in your web service. First of all they should be web methods with “Web Method” keyword at the top. Second, they should be declared “public” so that they can be accessed publically. And last but not least, you must need to rebuild solution of your web service when you have created a new web method and you want to make that web method available to the users of web service.