articles

Home / DeveloperSection / Articles / Web Service in .NET

Web Service in .NET

AVADHESH PATEL8323 24-Jul-2012

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 as

1.       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

Web Service in .NET

Step4:- Create a Web Site by File > New > Web Site > Asp.net Web Site and create UI as per as below format

Web Service in .NET

Step5:- In Visual Studio 2008, Right Click on Solution Explorer and Choose "Add Web Reference".

Web Service in .NET

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”

Web Service in .NET

Web Service in .NET

Web Service in .NET

Step6:- Paste copy string in “URL” box, then click button “go”. After few second button “Add Reference” enable then click on it.

Web Service in .NET

Step7:- After adding “Add Reference”, Solution Explorer look like this…..

Web Service in .NET

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”

Web Service in .NET



Updated 07-Sep-2019
Avadhesh Kumar Patel District Project Manager - Aligarh 14 months work experience in Panchayati Raj Department Sector as District Project Manager & 12 months work experience in IT Sector as Software Engineer. :-)

Leave Comment

Comments

Liked By