Shows and explains you how to make an ASP .NET Web Service, an ASP .NET Web Application and how to consume the Web Service with the ASP .NET Web Application.
Web Services represent a new programming concept. I’m not going to give you a description of Web Services, what they are and what they can do for you. There’s a beautiful explanation of Web Services at MSDN (see the Flash presentation).
I assume that you know at least the basics of C# and you are reading this tutorial because you want to learn how to create and consume a Web Service. So let’s get into it.
Creating the ASP .NET Web Service
Create a new ASP .NET Web Service at the location http://localhost/firstService.
After you create the new project Visual Studio opens the Web Service designer. We don’t have anything to design for the Web Service so let’s get to coding.
Add a new class to the project, leave the default Class1.cs name. As in any newly created class, the following code is created automatically
using System; namespace firstService { Â Â Â /// |
In this example we’ll make a method that converts all the characters in a string to uppercase. Of course it’s crazy to make a method just for using ToUpper() but the purpose of this tutorial is to teach you how to make a Web Service and a Web Application communicate.
Replace the code in Class1.cs with the code below. I suppose you already have an idea of what this code means, although I added comments to be more easy to follow.
using System; |
Now let’s get to the code in Service1.asmx.cs. The part that we need to modify is the commented one:
// [WebMethod] |
Replace the code with the following:
// Create a new Class1 object stored in cls1 |
As you can see in the above code we use Class1 to create a new object cls1. Further we declare a method Class1 that takes the string str argument. Inside it sets the property, it calls a method and gets a property of cls1, all by using the code in Class1.
Run the project and see the result:
Click Class1 link and you will be asked for input:
And here is the result:
It’s time to get yourself a beer or whatever you drink, next we’re creating the Web Application.
Creating the ASP .NET Web Application
Let’s get back to work. Create a new ASP .NET Web Application project in Visual Studio at http://localhost/firstApplication.
Again Visual Studio starts the project in design mode. This time we’ll do a little design before jumping into the code.
Drag 2 textBoxes and a button on the form. Name the first textBox txtInput, the second txtOutput and the button btnTransform.
Consuming the ASP .NET Web Service
Before we code we need to do one more thing – add a Web Reference to the Web Service. So in the Solution Explorer right click the project and choose Add Web Reference. Now we need to enter the address where the Web Service resides. In this project the Web Service is located at http://localhost/firstService/Service1.asmx. So add this into the text box and press enter. As you saw the Service1.asmx page opens.
Click Add Reference and we’re done with adding the Web Reference. Look into the Solution Explorer and there’s our reference:
Now double-click the button and we get to the btnTransform_Click event. Here we’ll use the following code.
private void btnTransform_Click(object sender, System.EventArgs e) |
localhost is of course, the name of the reference.
Run the code and
here’s the result:
That’s all