Jun 26, 2010

How to write a webservice and consume in same web apllication

Steps for writing web service

  1. go to solution explorer right click on project and add new item
  2.  select web service and change the name as mathservice.asmx
  3. opean the mathservice.cs and write webmethoods
mathservice.cs

using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

/// <summary>
/// Summary description for mathservice
/// </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 mathservice : System.Web.Services.WebService {

    public mathservice () {

        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

    [WebMethod]
    public int add(int x, int y)
    {
        return (x + y);
    }

    [WebMethod]
    public int mul(int a, int b)
    {
        return (a * b);
    }
   
   
}

4.right cilck on math service.asmx and view in browser and test the service
5.add one more new item  web form 
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <table align="right" class="style1">
        <tr>
            <td align="right">
                A</td>
            <td>
                <asp:TextBox ID="txta" runat="server"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td align="right">
                B</td>
            <td>
                <asp:TextBox ID="txtb" runat="server"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
                <asp:Label ID="lblres" runat="server"></asp:Label>
            </td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
    </form>
</body>
</html>

6.right click on solution explorer and add webreference
you can view

Start Browsing for Web Services

Use this page as a starting point to find Web services. You can click the links below, or type a known URL into the address bar.

Browse to:

select Web services in this solution option   for consuming webservice,the webservice automatically added  then you use the service

default.aspx.cs 


using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using math;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        mathservice m = new mathservice();
        if (txta.Text != "" && txtb.Text != "")
        {
            int ans1 = m.add(int.Parse(txta.Text), int.Parse(txtb.Text));
            lblres.Text = "addition:" + ans1.ToString();

        }

    }
}

















No comments:

Post a Comment