Tuesday, 23 June 2015

How to Create a Simple Web Service and Use it in ASP.Net


How to create a Web Service

Step 1
Go to Visual Studio then click on "File" -> "Website" -> "ASP.NET empty website template".
Then provide the website name (for example: WebServiceSample).


Step 2: Add a Web Service File

Go to Solution Explorer, then select the solution then click on "Add new item".

Choose the Web Service template.

Enter the name (for example: Calculation.cs) then click on "Add".


 



This will create the following two files:
  1. Calculation.asmx (the service file)
  2. Calculation.cs (the code file for the service; it will be in the "App_code" folder)

Open the file Calculation.cs and write the following code:

 using System;
 using
 System.Collections.Generic;
 using
 System.Linq;
 using
 System.Web;
 using
 System.Web.Services;
 ///
 <summary>
 ///
 used for Calculationcalculation
 ///
 </summary>
 [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 Calculation: System.Web.Services.WebService
 {
     public
Calculation()   {
         //Uncomment the following line if using designed components
         //InitializeComponent();
     }
     [WebMethod]
     public int Add(int x, int y)
     {
         return x + y;
     }
     [WebMethod]
     public int Sub(int x, int y)
     {
         return x - y;
     }
     [WebMethod]
     public int Mul(int x, int y)
     {
         return x * y;
     }
     [WebMethod]
     public int Div(int x, int y)
     {
         return x / y;
     }
 }

Step 3

To see whether the service is running correctly go to the Solution Explorer then open "Calculation.asmx" and run your application.

Now you will find all the method names in the browser.



To determine whether the functions are working, click on one of the functions (for example: "Add").

Now you will see two TextBoxes for checking. Enter the value for x and y and click on the "Invoke" button.

Now you will see the result in an open standard form (XML).


Now your service is ready for use.

Step 4: Creating the client application

Now create a website and design your form as in the following screen.




Source code  

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>
                        <asp:Label ID="Label1" runat="server" Text="Enter the First number "></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </td>
                </tr>

                <tr>
                    <td>
                        <asp:Label ID="Label2" runat="server" Text="Enter the First number"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    </td>
                    <td>
                        <asp:Label ID="Label3" runat="server" ></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="Button1" runat="server" Text="Add" OnClick="Button1_Click" />
                    </td>
                    
                    <td>
                        <asp:Button ID="Button2" runat="server" Text="Sub" OnClick="Button2_Click" />
                    </td>
                    
                    <td>
                        <asp:Button ID="Button3" runat="server" Text="Div" OnClick="Button3_Click" />
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    </td>
                    
                    <td>
                        <asp:Button ID="Button4" runat="server" Text="Mul" OnClick="Button4_Click" />
                    </td>
                </tr>

            </table>


        </div>
    </form>
</body>
</html>

Step 5: Add a web reference to the Website

Go to Solution Explorer then select the solution then click on "AddWeb Reference" then within the URL type the service reference path.

(For example: http://localhost:65312/WebServiceSample/Airthmatic.asmx) then click on the "Go" button.

Now you will see your service methods. Change the web reference name from "localhost" to any other name as you like (for example: WebAirthmatic).

Click on the "Add Reference" button. It will create a Proxy at the client side.




Now go to the cs code and add a reference for the Service.

Example:  using WebAirthmatic;


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebArithmetic;

public partial class test : System.Web.UI.Page
{

    CalculationSoapClient obj = new CalculationSoapClient();
    int a, b, c;

    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        a = Convert.ToInt32(TextBox1.Text);
        b = Convert.ToInt32(TextBox2.Text);

        c = obj.Add(a, b);
        Label3.Text = c.ToString();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        a = Convert.ToInt32(TextBox1.Text);
        b = Convert.ToInt32(TextBox2.Text);

        c = obj.Sub(a, b);
        Label3.Text = c.ToString();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        a = Convert.ToInt32(TextBox1.Text);
        b = Convert.ToInt32(TextBox2.Text);

        c = obj.Div(a, b);
        Label3.Text = c.ToString();
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        a = Convert.ToInt32(TextBox1.Text);
        b = Convert.ToInt32(TextBox2.Text);

        c = obj.Mul(a, b);
        Label3.Text = c.ToString();
    }
}






No comments:

Post a Comment