Web service is a method of communication between two web based application using XML, SOAP, WSDL and UDDI open standards over an internet protocol backbone. Web service can convert your application into web application which can publish its function or message to the rest of the world). Web service are-
- Web services are application components
- Web services communicate with open protocols
- Web services are self-contained and self-describing
- Web services can be discovered using UDDI (*4)
- Web services can be used by other applications
- XML is the basis of web services
Why Web Services?
Different web applications in the web want to interact with each other. Such as application can share data, can invoke method of other application. So, how can you solve this? It will also be a great problem if applications are in different platform and language. Web service solves the above issues.
By using Web services, your application can publish its function or message to the rest of the world. Web services use XML to code and to decode data, and SOAP to transport it (using open protocols). With Web services, your accounting department's Win 2k server's billing system can connect with your IT supplier's UNIX server
Web services have two types of uses:
Reusable Application Component - currency conversion, weather reports, language translation.Connect Existing Software - Web services can help to solve the interoperability problem by giving different applications a way to link their data.
How does it work?
The basic web service platform is XML + HTTP. Web service elements are –
- SOAP – Simple Object Access Protocol
- UDDI – Universal Description, Discovery and Integration
- WSDL – Web Service Description Language
With ASP.net, you do not have to write SOAP and WSDL documents. ASP.net automatically creates SOAP and WSDL documents.
Example:
Simple Addition Method: Here I have tried to represent web service before you by a simple add method which takes two parameter as two integer value and it returns the addition of these two.
Step 1: Create Asp.net Web service
2. Click right button on asp.net web site.
3. Click on add new Item.
4. Select Web service like the below screen.
5. Right click on WebService.asmx and select view code. You will see the following code snippet. It should be mentioned here that I have created my web service named WebService which you have already seen in previous screen.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
///
/// Summary description for WebService
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
public WebService () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
6. Simply add a Method name Add as like below.
/// Copyright : Copyright© mahedee.hasan@gmail.com. All rightsreserved.
/// NameSpace :
/// Class : WebService
/// Inherits : System.Web.Services.WebService
/// Author : Md. Mahedee Hasan
/// Purpose : This is a simple web service to add two number
/// Creation Date : 21/01/2012
/// =========================================================================================
/// || Modification History ||
/// ------------------------------------------------------------------------------------
/// Sl No. Date: Author: Ver: Area of Change:
/// 1. 21/01/2012 Mahedee 1.0 Created
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
///
/// Summary description for WebService
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
public WebService () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public int Add(int firstVal, int secondVal)
{
return firstVal + secondVal;
}
}
7. .Click right button on WebService.asmx and select Set As Start Page
8. Run Project or press F5. You will see the following screen.
9. Click Add method, you will see the following screen.
10. Suppose I input two value 12 and 30. Click Invoke.
11. After clicking Invoke you will get the addition of two values as XML format.
Now your web service is ready to use. We have to use web service in various ways. In the next step I will show how to access web service from client page.
Step 2: Create a client page and access web service.
There are many ways to consume web service. Here is an example of HTML-POST protocol which is one of the ways of consuming web service.
1. Create a aspx/html page. I have used here an aspx page name “ClientForm.aspx”.
2. Add the following tags in aspx source.
<form id="form1" runat="server" action="http://localhost/Demo_2010/WebService.asmx/Add">
<div>
<input name="firstVal">input>
<input name="secondVal">input>
<input type="submit" value="Enter"> input>
div>
form>
3. In action attribute of form tag, I have used localhost because I have already hosted it. You can simply use “http://localhost:5143/Web/WebService.asmx/Add” in action attribute instead of present value in action attribute if you didn’t host website in your local machine. Here 5143 can vary.
4. Click right button on ClientForm.aspx and select Set As Start Up page.
5. Run your project.
6. You will see the following screen.
7. Enter two values of two textbox.
8. Press Enter you will get the result in xml format.
<int xmlns="http://tempuri.org/">25int>
This is the basic example and web service. You will see more about web service next.
Notes:
1. XML provides a language which can be used between different platforms and programming languages and still express complex messages and functions.
2. SOAP: The HTTP protocol is the most used Internet protocol.
SOAP is an XML-based protocol to let applications exchange information over HTTP.
Or more simple: SOAP is a protocol for accessing a Web Service.
- SOAP stands for Simple Object Access Protocol
- SOAP is a communication protocol
- SOAP is a format for sending messages
- SOAP is designed to communicate via Internet
- SOAP is platform independent
- SOAP is language independent
- SOAP is based on XML
- SOAP is simple and extensible
- SOAP allows you to get around firewalls
- SOAP is a W3C standard
3. WSDL:
WSDL is an XML-based language for locating and describing Web services.
- WSDL stands for Web Services Description Language
- WSDL is based on XML
- WSDL is used to describe Web services
- WSDL is used to locate Web services
- WSDL is a W3C standard
4. UDDI:
UDDI is a directory service where companies can register and search for Web services.
- UDDI stands for Universal Description, Discovery and Integration
- UDDI is a directory for storing information about web services
- UDDI is a directory of web service interfaces described by WSDL
- UDDI communicates via SOAP
- UDDI is built into the Microsoft .NET platform
I am very beginner in ASP.net. This is very helpful topic. Please write something very basic level of ASP.net and visual studio. Thanks again for your valuable post.
ReplyDeleteThanks. Yes I will write some blog in very basic level for beginner soon. Please keep in touch.
ReplyDelete