Monday 18 March 2013

Web Service In ASP DOT NET



Web Service

Web Service is an application that is designed to interact directly with other applications over the internet.



1. Open Visual Studio.
FILE -> NEW -> Web Site -> Select 'ASP.NET Web Service'
You Will Get ‘Service.cs’ As Below.

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

[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 () {

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

//----------------------------------------
//** Don’t Forget To Add [WebMethod] Above Any Method. This Certify That This //Method Begongs To Web Service.
----------------------------------------

//----------      Create Method Such As  --------------
// This Method Is Written By Me And All Remaining Are Default On This Page.
    [WebMethod]
    public int GetME(int v1, int v2)
    {
        return v1 * v2;
    }

//-------------------------------------------------

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
}

2. Browse This And You Will Get A Page Where You Find Methods Written By You And Some Defaults like HelloWorld().

** If Your Get Error While Browsing Then Browse It By Right Click Over Service Name In While Is Inside Solution Explorer.

Click On Your Method (GetME) And Check That Is It Properly Working Or Not.


3. Now Copy URL 

http://localhost/MyWebService/Service.asmx

4.  Open Another Project And Select WebPage Named - > ex.  'Consume_MyService'

5. Right Click On Project Name in Solution Explorer And Select 'Add Web Refrence'.

6. A Pop Up Will Open 'Add Web Refrence' Here You Paste Above Written URL Address in URL ( i.e. you web service address.)
And Clik on Go Button.

7. You will Get Here Web Service Name In This Pop up in Right Side , You may Change It. Click ON 'Add Refrence' Button.

This Will Added To 'App_WebReferences' folder.

8.  Now On 'Consume_MyService.aspx'
<div>
    <asp:TextBox ID="txtV1" runat="server" />
    <asp:TextBox ID="txtV2" runat="server" />
    <asp:TextBox ID="txtResult" runat="server" />
    <asp:Button ID="btnGet" runat="server" Text="Multiply" />
    </div>

9. On Button Click Event on 'Consume_MyService.cs' page.

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

    localhost.Service mywebservice = new localhost.Service();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnGet_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txtV1.Text) && !string.IsNullOrEmpty(txtV2.Text))
        {
            int v1 = Convert.ToInt32(txtV1.Text);
            int v2 = Convert.ToInt32(txtV2.Text);
            int result = mywebservice.GetME(v1, v2);
            txtResult.Text = result.ToString();
        }
    }
}

10.  Run Your Page.


**  If You Add More Function To Web Service Then Again Run That And Repeat Same Process On 'Consume_MyService.aspx' Page.
Means You Have To Replace Previous Used Web Serivce. While This Process You May Get Message Box Just Click YES, YES &, YES.
Means Have To Repeat  - >Setps 5, 6 &, 7.

Cheers!!! 

No comments:

Post a Comment