web services in c#

Introduction of web services

The term "web service" has been used quite frequently lately: you hear people saying how good web services are and how they will dominate the future of software development, but what exactly are web services and how can we create them? In this we will explore the features of Microsoft ASP.NET Web Services, more specifically how to build web services. A real world example extending what I discuss in this article will be shown at the end of this . To understand this webservices fully, you are required to have some previous knowledge of C#, ASP.NET . To try the examples shown in this article for yourself, you will need the .NET Framework and Internet Information Server 5 or higher installed on your Windows NT/XP/2000/vista PC.

What is a web service?
The term "web service" refers to a form of a component that can be used remotely. Microsoft offers two types of web services in their .NET framework: XML web services and .NET remoting. When developers refer to web services they usually mean XML web services, and in this article I will also refer to XML web services as just web services. Web services are invoked remotely using SOAP or HTTP-GET and HTTP-POST protocols. Web services are based on XML and return an "answer" to the client in XML format. Web services have all the advantages of components plus many more. The most significant benefits include:

Language and platform independence: Web services can be built and consumed on any operating system just as long as that operating system supports the SOAP protocol and XML.

Automatic upgrade: Unlike components, if a web service requires an update, that update is propagated to all applications consuming that web service immediately. This is because the actual methods and properties for the web service are invoked from the web server remotely, meaning that each function contained within a web service appears as a "black box" to a client: they aren't concerned with the way the function does its job, just as long as it returns the expected result.

How Does it Work?

The basic Web services platform is XML + HTTP.
The HTTP protocol is the most used Internet protocol.
XML provides a language which can be used between different platforms and programming languages and still express complex messages and functions.
Web services platform elements
• SOAP (Simple Object Access Protocol)
• UDDI (Universal Description, Discovery and Integration)
• WSDL (Web Services Description Language)

Commonly Used Jargon
Here's a list of some terms that you'll come across as you begin of the web services


UDDI (Universal Description, Discovery and Integration)
This isn't the case at the moment, but I'm pretty sure that in the future there will be thousands of web services on the Internet. The question is how are we going to find the web services we are looking for? UDDI is the answer to that question. UDDI is a registry that provides a place for a company to register its business and the services that it offers. People or businesses that need a service can use this registry to find a business that provides the service.

When you search for a web service using UDDI's web service or web browser, UDDI returns a listing of web services that matched your criteria. This list is returned in the form of a DISCO or WSDL document.


WSDL (Web Services Description Language)
WSDL is a language that describes a web service. It contains information such as where you can find the web service, methods and properties that it supports, its data types, and the protocol used to communicate with the web service. WSDL is based on the XML format and it's used to create proxy objects. Basically, without a WSDL document, developers wouldn't be able to use web services simply because they wouldn't know which methods and properties they support and also which communication method any particular web service supports.


DISCO is a list of WSDL documents. DISCO is used to group common web services together. DISCO documents are also in XML format.


SOAP (Simple Object Access Protocol) is a protocol to transport data to and from the web server. It is in XML format and allows you to transport a variety of data types used in .NET. As an alternative to SOAP, we can use HTTP-GET and HTTP-POST, which will be covered later in the article. These protocols return the output in a non-SOAP format, however this output is still in XML format. Well, this isn't the case at the moment, but I'm pretty sure that in the future there will be thousands of web services on the Internet. The question is how are we going to find the web services we are looking for? UDDI (Universal Description, Discovery and Integration) is the answer to that question. UDDI is a registry that provides a place for a company to register its business and the services that it offers. People or businesses that need a service can use this registry to find a business that provides the service. When you search for a web service using UDDI's web service or web browser, UDDI returns a listing of web services that matched your criteria. This list is returned in the form of a DISCO or WSDL document. WSDL WSDL (Web Services Description Language) is a language that describes a web service. It contains information such as where you can find the web service, methods and properties that it supports, its data types, and the protocol used to communicate with the web service. WSDL is based on the XML format and it's used to create proxy objects. Basically, without a WSDL document, developers wouldn't be able to use web services simply because they wouldn't know which methods and properties they support and also which communication method any particular web service supports. DISCO DISCO (Abbreviated from discovery) is a list of WSDL documents. DISCO is used to group common web services together. DISCO documents are also in XML format. SOAP SOAP (Simple Object Access Protocol) is a protocol to transport data to and from the web server. It is in XML format and allows you to transport a variety of data types used in .NET. As an alternative to SOAP, we can use HTTP-GET and HTTP-POST, which will be covered later in the article. These protocols return the output in a non-SOAP format, however this output is still in XML format.

We now show you how to create the Helloworld Web service. In the following steps, you will create an ASP.NET Web Service project that executes on your computer's local IIS Web server. To create the Helloworld Web service in Visual Studio 2005, perform the following :

Creating an ASP.NET Web Service in Visual Studio 2005



HelloWorldMethod. HelloWorldMethod returns a string, "Hello World". We'll add modifications to this web service as we learn more about the available features of web services.

Before building a web service, a virtual directory or web application must be created using IIS

The following code shows the most basic web service:

using System.Web.Services;
public class HelloWorld : WebService
{
[WebMethod]
public string HelloWorldMethod()
{
return "Hello World";
}
}

Copy the code above into your favourite text editor and save the file into the virtual directory we created above as HelloWorld.asmx. The file extension for a web service is .asmx.

You can now access our HelloWorld web service in your browser by visiting http://localhost/HelloWorld/HelloWorld.asmx.

You should see a page that looks like this:



This web page is automatically built by IIS using the WSDL of our web service. You can change how this page looks but that's out of the scope for this article. You can view the raw WSDL by clicking on the link to "Service Description", which will forward you to the WSDL for our web service.

As you can see, the WSDL shows where you can find the web service (URI) and other useful information including the methods that we have created in our web service. Now that we've built the web service, we need to be able to use it. On the main web service page, you can see the method that we've created. If you click on the method, it will forward you to this page:



When you click on the invoke button, you'll get the following result: