Exforsys.com
 

Sponsored Links

 

ASP.NET Tutorials

 
Home Tutorials ASP.NET
 

Creating and consuming XML Web Services with C#

 

Creating and consuming XML Web Services with C#

Page 1 of 2
Introduction: We all talk about webservices, webservices can do this and webservices can do that. But when we are asked to make one, we hesitate. Maybe it's because we never made a webservice before, and all the time playing with Webforms and Windows Forms or even Console Applications. By the way, I love Console applications. In this article, I will show you how to create a simple webservice that is consumed by a Console application client.


Motivation of the XML Web Services

Most of the people does not understand that why we need a web service and make a wrong use of it. The main idea of a web service is to join two businesses together since they cannot join due to their graphical locations. Web Service is also used to link different systems together. The best thing about this is that the systems can be in different in nature. Meaning a web service enables a windows application to interact and communicate with the linux application. This is done by using XML as the transferring medium. Since XML is understood by all the systems because its nothing but plain text that's why this is a perfect language to join systems together. XML web Service should never be used to transfer confidential data, not to be used in real time programming where time is the essence.


Making the Web Service

First, start your Visual Studio .NET, and in the project type, select ASP.NET WebService. In the left pane, choose the language of your choice. In this example, I will be using Visual C#.NET. Once you select the project, a page will appear which will be more like a design page, switch to its code view. In the code view, you can see lot of comments and C# code already written for you. You will also see that at the bottom, there is a method HelloWorld which is written for you by default, so you can test your service and of course say hello to the world. After removing the unwanted code and comments, your code will look like this:


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace WebServiceExample
{
public class Service1 : System.Web.Services.WebService
{
public Service1()
{

InitializeComponent();
}


// Add the Description Attribute so you
// will know the purpose of your WebService
[WebMethod(Description="This Method prints HelloWorld")]
public string HelloWorld()
{
return "Hello World";
}
}
}


Let's dig into this small code. [WebMethod] Attribute denotes that this method will be used by the clients, also this method has to be public in order for a client to use it. Description inside the WebMethod Attribute just gives the method more meaning. Don't worry about the InitializeComponent() method since it's written by default.


Running the Web Service


OK, now you have made your first kick ass WebService (without even writing a single line of code). Let's run it and check whether it gives the correct result or not. In the Solution Explorer, right click on the .asmx file, and select View in Browser as shown below:



Once you click on "View in Browser", the next screen you will see will be something like this:



have erased most of the stuff, so you can only see the method that you need in this example. Below, you can see the method HelloWorld and the description you wrote for the method.

Now, click on the HelloWorld method. I don't want to scare you with all the SOAP and HTTP code produced, so I am only going to paste the screen shot which will be relevant to this example.




Alright, so far so good. Now, just press the Invoke button to see the result of your method named HelloWorld().



This is cool. You have just tested your first webservice and it ran since you didn't coded it. I know what you are thinking right now. Is the client going to see the result like this strange format (this is XML format). Well, of course not. That's why you need to make a Proxy class which consumes this service.


Next Page: Creating and consuming XML Web Services with C# - Page 2


Read Next: ASP .NET Migration and Interoperability



 

 

Comments


debu_2005_abcd said:

  very helpful
May 25, 2005, 10:28 am

mythili said:

  worked like a jump starter. ver very helful
March 7, 2007, 3:27 pm

Prachi 123456 said:

  Good to start with!
December 9, 2007, 10:55 pm

philip12345 said:

  Very Very good introduction to webservices. I have read a lot of complex articles that make it too boring to learn about webservices, They just make the reader irritated . You have given a simple programme to create and consume webservices. Excellent ! simply Excellent!!
April 3, 2008, 11:45 am

Manoj12345 said:

  This article is a very good starter. I was searching the Web for a good article on Web Services because i could not understand what they are speaking? After reading this article I have a got a very good idea about WebService. Thanks to Exforsys and author of this article for explaining WebServices in a simple and sweet manner.
Excellent article!!!
May 13, 2008, 9:03 pm

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape