This Tutorial will teach you how to consume a Web Service in ASP.NET

Introduction

This tutorial will teach you how to Consume a Web Service. Consuming Web Services can be compared to Inheritance for the web. This sample will call a HelloWorld function from a web service in a different project using a reference to our service.

Step 1. Create the Service and get Info

We have already created a Simple Web service in another tutorial (here). Once you have created the web service we need to run it and get the URL that it is running at in our example (“http://localhost:56671/MyService.asmx”) is the URL of our service. Copy the URL we will need it in a minute.

Step 2. Creating the Reference

After we have the URL we need to add a reference. We do this by Right clicking on the Project in VS 2010 and selecting Add Web Reference Paste the URL that we copied in step one over to the address bar and click the green arrow. It should load a preview of the Service you created. After that name the Service “MyService” and click OK. Now that we have a reference to the Service we can use it to create an Object to Call our functions in C#(or VB). The following code will use the reference to create an Object and use it to call the HelloWorld() Function.

Conclusion

In this Tutorial we have learned how to Consume a Web Service in ASP.NET using C#. This can be very useful for large scale websites that have many functions stored in Services to help organize your projects.

Download Source Files