DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 Consuming a Web Service

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.

Need help with cloud hosting? Try Server Intellect. We used them for our cloud hosting services and we are very happy with the results!

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.

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

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.

	// Make an instance of the WebService Class
	// using the Web Reference we provided
	MyService.MyService service = new MyService.MyService();
	// from the Hello World Function in this case "HelloWorld"
	string message = service.HelloWorld();
	//display the message to the screen
	Label1.Text = message;
	

We chose Server Intellect for its cloud servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish, and their customer service is stellar.

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 Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!