Home > DeveloperSection > Articles > Web Services in .Net

Web Services in .Net


Web Services Web Services 
Ratings:
0 Comment(s)
 3677  View(s)
Rate this:

Web Services in .Net

A web service allows a site to expose programmatic functionality via the Internet. Web services can accept messages and optionally return replies to those messages.

Creating a Web Service

This example shows step-by-step how to create a web service in Visual Studio 2010.

1.      Start Microsoft Visual Studio 2010. Click File > New > WebSite. Following Window will appear.

Web Services in .Net

Select the "Visual C#" node under "Project Types". Under Templates, select "ASP.NET Web Service" and name the project "WebServiceSample". Click OK Button.

2.      When you press OK the following window will appear.

Web Services in .Net

We can see that few code of lines are automatically written here. There is a demo function named HelloWorld (), when it will be called it will return a value “Hello world”. This function HelloWorld () is just for a reference to how to make a new function. If it is not require, you can also remove/comment it.

Now, here we have to do our coding according to our task.

Here, I am creating a function performOperation(),which will accept three parameters operation,number1,number2 sequentially and will return the value of calculation as double data type.

3.     Code

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

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 : System.Web.Services.WebService

{

    public Service () {       

    }   

    [WebMethod]

    public double performOperation(string operation,double number1,double number2)

        //this will accept three parameters and return the result of calculation

    {

        double result;

        switch (operation)

        {

                case "Add":

                    result = number1 + number2;

                    break;

                case "Subtract":

                    result = number1 - number2;

                    break;

                case "Multiply":

                    result = number1 * number2;

                    break;

                case "Divide":

                    result = number1 / number2;

                    break;

                default:

                    result = 0;

                    break;

        }

        return result;   

    }

}

 

4.      Now, we need to compile the application and add the compiled application to IIS in 

Web Services in .Net

5..      Now, Create a New ASP.Net Website to demonstrate the Web Service.

Open Visual Studio 2010 > File > New >ASP.NET website and provide it website name, and design the following User Interface

 

Web Services in .Net

 

6.      We need to add the web reference of the web service. To add web reference of the web service right click in “Solution Explorer” and select “Add Service  Reference”,   add web reference dialog box will appear. There add the web reference of the service in my case it is http://localhost:6971/WebServiceSample/Service.asmx

 

Web Services in .Net

 

On Clicking, there will be following window opened.

Web Services in .Net

 

Click on Advance Button, Following window will open

 

Web Services in .Net

 

Now, Click on Add Web Reference Button, following window will be open.

Web Services in .Net

Paste copy string in “URL” box, then click button “go”. If service found then click on Add Reference Button.

 

After Successful adding the Service Reference, you can see it in your Solution Explorer.

Web Services in .Net

 

7.      Now, back to coding part, and add the following code on the Click Event of the Get Result Button.

  using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

public partial class _Default : System.Web.UI.Page

{

    double result;

    localhost.Service ws = new localhost.Service();

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        //Response.Write(cbOperation.SelectedValue.ToString());

      

             

        if (Convert.ToInt32(txtN2.Text) == 0 && cbOperation.SelectedValue == "Divide")

        {

            Response.Write("Can't Divide a Number by 0 ");

            return;

        }

       

        result= ws.performOperation(cbOperation.SelectedValue.ToString(), Convert.ToInt32(txtN1.Text), Convert.ToInt32(txtN2.Text));

        //passing the parameter to the Web Service's Method

        Response.Write(result.ToString());

        // it will get the result from web service and will print it.

    }

  

}

 

 

8.      Run the Website  > Enter the First and Second Number in corresponding text box and the select the desired operation wish to perform and click on Get Result Button.

 

Web Services in .Net


Don't want to miss updates? Please click the below button!

Follow MindStick