A Simple Example of Web Service

In this article we will discuss how to create a Web Service, deploy it and use it in our application.

How to Create a Web Service

Follow this procedure to create a simple Web Service.

Step 1

First we will click on "File" -> "New" -> "Project..." -> ASP.NET Web Service Application like this:

WebService1.jpg

Step 2

The Service1.aspx.cs page will be opened; here we will write the following code:

namespace CalcWebService

{

   

    [WebService(Namespace = "http://tempuri.org/")]

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    [ToolboxItem(false)]

    public class Service1 : System.Web.Services.WebService

    {

 

        [WebMethod]

        public double Add(int a, int b, int c)

        {

            int sum;

            sum = a + b + c;

            return sum;

        }

    }

}

This namespace is the default namespace, we can change it as needed. Here we create a WebMethod (Add) , to define the functionality of the WebService. We can also send and receive messages using the WebMethods over the internet.

Step 3

Now we will execute the WebService (press F5 Key).

The output will be:

WebService2.jpg

Now we will click on Add:

WebService3.jpg

Now we enter some values in it:

WebService4.jpg

Now we will click on the Invoke Button. The result will be shown in a new page like this:

WebService5.jpg

Deploying The Web Service

Now we will look at this, how to deploy the web service, in other words how to make this web service available for the other web applications over the internet.

Step 1

First we will open our Web Service in the Visual Studio and then we will click on "File" -> "New" -> "Project..." like this:

WebService6.jpg

A window will be shown; here we will select the Other Project Types and then Setup Project:

WebService7.jpg

When we will click the Ok Button the following window will be shown:

WebService8.jpg

Here we click on "Add" -> "Project Output". After that the following window will be shown where we select:

WebService9.jpg

After that right-click on the CalcSetup and then select the Build Option like this:

WebService10.jpg

So when we buid the project the CalcSetup folder populates the two folders (Debug and Release).

WebService11.jpg

Now double-click on the Debug folder and run the exe file:

WebService12.jpg

Now click on the Next Button:

WebService13.jpg

After that install the CalcSetup, after the installation it will be available over the internet.

How to use this WebService in an Application

In order to use this WebService in an application, we create a project that contains this Web Service. After that we create a .aspx page Adddata.aspx.

Here we will use the following controls to show our data:
 

<table >

            <tr>

                <td class="style2">

                </td>

                <td class="style2">

                </td>

            </tr>

            <tr>

                <td class="style3">

                    First Value</td>

                <td>

                    <asp:TextBox ID="txtFirst" runat="server"></asp:TextBox>

                </td>

            </tr>

            <tr>

                <td class="style3">

                    Second Value</td>

                <td>

                    <asp:TextBox ID="txtSecond" runat="server"></asp:TextBox>

                </td>

            </tr>

            <tr>

                <td class="style3">

                    Third Value</td>

                <td>

                    <asp:TextBox ID="txtThird" runat="server"></asp:TextBox>

                </td>

            </tr>

            <tr>

                <td class="style3">

                    &nbsp;</td>

                <td>

                    <asp:Button ID="btnAdd" runat="server" onclick="Button1_Click" Text="Add" />

                </td>

            </tr>

            <tr>

                <td class="style3">

                    Output</td>

                <td>

                    <asp:TextBox ID="txtOutput" runat="server"></asp:TextBox>

                </td>

            </tr>

        </table>

WebService14.jpg

So when we click on the Add Button the output will be shown in the TextBox. For this, first we add the Web Reference in our project, for this right-click on the CalcWebService Project and Select Add Web Reference like this:

WebService15.jpg

The following window will be shown:

WebService16.jpg

Here we will select Web Services in this solution:

WebService17.jpg

When we click on the Service1:

WebService18.jpg

Now we will click on the Add reference Button. So the output will be:

WebService19.jpg

Now we will write the code on the Add Button to use this Web service like this:
 

            protected void btnAdd_Click(object sender, EventArgs e)

        {

            localhost.Service1 ws = new CalcWebService.localhost.Service1();

            ws.EnableDecompression = true;

            double sum = ws.Add(System.Convert.ToInt16(txtFirst.Text), System.Convert.ToInt16(txtSecond.Text), System.Convert.ToInt16(txtThird.Text));

            txtOutput.Text = sum.ToString();

 

        }

The output will be:

WebService20.jpg
 


Similar Articles