Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

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.

On Monday, June 28th 2004 at 02:29 AM
By Andrew Pociu (View Profile)
*****   (Rated 4.8 with 10 votes)
Contextual Ads
More C# Resources
Advertisement
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:





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 .
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Rajeev G. on Thursday, October 28th 2004 at 03:45 AM

Excellent article...but i couldn't understood the use of Class1 in the example when the web service can be tested without that also.

by Kelvin Y on Tuesday, December 14th 2004 at 08:15 AM

A good basic tutorial, but spoiled by the poor use of naming for classes and methods.

by Dana on Friday, January 21st 2005 at 10:42 AM

Mine didn't work. I'm using vb.net though. The web service works and returns the correct information, but when I run the web application, I'll type in the sentanced to be transformed, and nothing will show in the txtOutput box. :(

by sherin on Monday, September 12th 2005 at 03:05 AM

i've been looking for an article on "how to create & consume a web service". This article was really useful & it worked fine for me.you have done a great job

by vishant on Tuesday, July 25th 2006 at 06:19 AM

This web service is located at the local machin. but what i will have to do consume the web service that is located on some another machin on the internet...?
You can also mail me @
vishantpatil@greymatterindia.com

by Edwin on Monday, April 23rd 2007 at 08:23 PM

Very easy to understand!Good work!

by verizon ringtones on Friday, August 17th 2007 at 03:12 PM

hello all

by NeYawn on Saturday, April 26th 2008 at 11:22 AM

I just built my first application.Thanks!

by Gopalan on Thursday, July 8th 2010 at 07:21 AM

Very nice article for beginners.

by Gopalan on Thursday, July 8th 2010 at 07:21 AM

Very nice article for beginners.

by TheTitsSucker on Tuesday, July 13th 2010 at 07:35 PM

Esta atoda madre este articulo para nosotros los principiantes

thanks

by RobertsonMaureen on Friday, July 16th 2010 at 08:04 PM

Some time before, I really needed to buy a good car for my firm but I didn't earn enough cash and could not purchase anything. Thank God my mate adviced to take the <a href="http://bestfinance-blog.com">loan</a> from trustworthy bank. Thus, I acted that and used to be happy with my sba loan.


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C# Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Sponsors
Discover Geekpedia

Other Resources