A 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.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This 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.
Creating 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.
Creating and consuming Web ServicesShows 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) |
|||||
|
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 ServiceCreate 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
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.
Now let's get to the code in Service1.asmx.cs. The part that we need to modify is the commented one:
Replace the code with the following:
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 ApplicationLet'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 ServiceBefore 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.
localhost is of course, the name of the reference. Run the code and here's the result: ![]() That's all . |
||||||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
||||||
|
||||||
Current CommentsExcellent article...but i couldn't understood the use of Class1 in the example when the web service can be tested without that also.
A good basic tutorial, but spoiled by the poor use of naming for classes and methods.
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. :(
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
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
Very easy to understand!Good work!
hello all
I just built my first application.Thanks!
Very nice article for beginners.
Very nice article for beginners.
Esta atoda madre este articulo para nosotros los principiantes
thanks
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.
Related Tutorials
Related Source Code
C# Job Search