Creating and consuming Web Services

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

{

   ///

   /// Summary description for Class1.

   ///

   public class Class1

   {

      public Class1()

      {

         //

         // TODO: Add constructor logic here

         //


      }

   }

}

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;
namespace firstService

{

   ///

   /// Summary description for Class1.

   ///

   public class Class1

   {

      private string str = "";

      // The Text property with

      public string Text

      {

         // Get the string using the property

         get

         {

            return str;

         }

         // Set the string to the value supplied

         set

         {

            str = value;

         }

      }

      // Method used to transform the string to uppercase

      public void upper()

      {

         // upStr holds the string transformed to uppercase

         string upStr = this.Text.ToUpper();

         // Apply the modifications

         this.Text = upStr;

      }

   }

}

Now let’s get to the code in Service1.asmx.cs. The part that we need to modify is the commented one:

// [WebMethod]

// public string HelloWorld()

// {

// return "Hello World";

// }

Replace the code with the following:

// Create a new Class1 object stored in cls1

Class1 cls1 = new Class1();

[WebMethod]

// Method Class1 with the str input

public string Class1(string str)

{

   // Set the Text property

   cls1.Text = str;

   // Call the method

   cls1.upper();

   // Get the property

   return cls1.Text;

}

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:

WebServiceWebApplication

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)

{

   // Create a new instance of the web service

   localhost.Service1 myService = new localhost.Service1();

   // Transform the string to uppercase

   string result = myService.Class1(txtInput.Text);

   // Display the result

   txtOutput.Text = result;

}

localhost is of course, the name of the reference.

Run the code and
here’s the result:

That’s all 

Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top