What is Web Service?
Web Service is an application that is designed to interact directly with other applications over the internet. In simple sense, Web Services are means for interacting with objects over the Internet.
History of Web Service or How Web Service comes into existence?
As I have mention before that Web Service is nothing but means for interacting with objects over the Internet.
1. Initially Object - Oriented Language comes which allow us to interact with two object within same application.
2. Then comes Component Object Model (COM) which allows interacting two objects on the same computer, but in different applications.
3. Then comes Distributed Component Object Model (DCOM) which allows interacting two objects on different computers, but within same local network.
4. And finally the web services, which allows two object to interact internet. That is it allows interacting between two objects on different computers and even not within same local network.
Example of Web Service
Weather Reporting: You can use Weather Reporting web service to display weather information in your personal website.
Stock Quote: You can display latest update of Share market with Stock Quote on your web site.
News Headline: You can display latest news update by using News Headline Web Service in your website.
Web Service Communication:
Web Services communicate by using standard web protocols and data formats, such as1. HTTP
2. XML
3. SOAP
Advantages of Web Service Communication
Web Service messages are formatted as XML, a standard way for communication between two incompatible systems. And this message is sent via HTTP, so that they can reach to any machine on the internet without being blocked by firewall.Here is one question arising in your mind and that is testing a Web Service? Answer is: You can test web service without building an entire client application. With Asp.net you can simply run the application and test the method by entering valid input parameters. You can also use .Net Web Service Studio Tool comes from Microsoft.
Example of Creating Web Service in .Net
This Web Service will take two numbers from client web page and add these on web service page then return to client page.
Step1:- Create a Web Service Application by File > New > Web Site > Asp.net Web Services
Step2:- Write a method named “Add” on “Service.cs” file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Services;
[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 Service : WebService
{
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
//creating method Add which will accept two arguments.
public int Add(int a, int b)
{
//returning value
return a + b;
}
}
Step3:- Build Web Service and Run the Web Service for testing by pressing F5 function key. Copy the URL string for “Add Web Reference” in your project
Step4:- Create a Web Site by File > New > Web Site > Asp.net Web Site and create UI as per as below format
Step5:- In Visual Studio 2008, Right Click on Solution Explorer and Choose "Add Web Reference".
Or in Visual Studio 2010, Right Click on Solution Explorer and Choose "Add Service Reference" then click on button “Advance”, then chose “Add Web Reference”
Step6:- Paste copy string in “URL” box, then click button “go”. After few second button “Add Reference” enable then click on it.
Step7:- After adding “Add Reference”, Solution Explorer look like this…..
Step8:- Write codes on button “Add” click event
using System;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
//creating object of service created earlier.
localhost.Service ws = new localhost.Service();
//using Add method of web service and displaying the result in the page.
Response.Write((ws.Add(int.Parse(txtNo1.Text), int.Parse(txtNo2.Text))).ToString());
}
}
Step9:- Run this web site and enter values in TextBox then click button “Add” and see output. E.g. 1st value is “5” and 2nd value is “15”. Output is “20”
Vijay Shukla
17-Apr-2013Hi Parvesh Singh!
In ASP.NET Web Service Application project template is not available for .Net framework 4.0, however, available for .Net Framework 3.5.
If you're building your application on .net framework 4.0, You can use WCF Service Application as ASMX in legacy. Please note that you'd need to enable AspNetCompatibilityMode to access HttpContext objects.
If you still want to use ASMX, choose ASP.NET Empty Web application and then you can add ASMX files to the project.
Pravesh Singh
17-Apr-2013Your article is very informative but when I'm trying to create web service in Visual Studio 2010, ASP.NET Web Service option is not show.