Steps to create and consume web service

Q.5) a) Write code and steps to create and consume web service.

Ans.

Step 1: Open Visual Studio 2010 and create a new web site.->Select .Net Framework 3.5. ->Select ASP.NET Web Service page -> Provide the name of your service. In this example I have given its name "WebServiceExample". Then Click the ok Button. A screen-shot of these activity is given below.

Step 2: A code window will open after Clicking the Ok Button which is given below. In this file by default HelloWorld Method is written. Modify this file and write your method according to your need. I have written two methods Add and Subtract. Every method should have attribute as [WebMethod].

using System.Web.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class Service : System.Web.Services.WebService
{
public Service () {

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


[WebMethod]
public int Add(int a,int b)
{
return a+b;
}
[WebMethod]
public int Subtract(int a, int b)
{
return a - b;
}

}

Step 3: Run the above code application.

Step 4: Now take a new web site to consume web service and provide some name I have given name as WebSerConsume. Now right click at your project in Solution Explorer. After doing this a pop - up menu will be appeared. -> select Add Web Reference.

Step 5: After clicking on Add Web Reference, a new window will be appeared:

Step 6: Copy the URL of your web service application and paste to it at place of URL and Click the Go Button or press enter.

Step 7: Now click the Add Reference Button. Now reference has been added to your application.
Now, write the code. I have added two Button control on design window of web page. Write the following code on Button Click event.
using System;

public partial class _Default : System.Web.UI.Page
{
protected void btnAdd_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(txtFirstNo.Text);
int b = Convert.ToInt32(txtSecondNo.Text);

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

txtResult.Text = obj.Add(a, b).ToString();
}
protected void btnSubtract_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(txtFirstNo.Text);
int b = Convert.ToInt32(txtSecondNo.Text);

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

txtResult.Text = obj.Subtract(a, b).ToString();
}
}

Now run the website.



b) Write a program to implement hit counter of the website by using session and Global asx file.

Ans.

Code for Global.asax file

< %@ Application Language="C#" %>

< script runat="server">

void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["NoOfUsers"] = 0;
}


void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Application.Lock();
Application["NoOfUsers"] = (int)Application["NoOfUsers"] + 1;
Application.UnLock();
}

Code for page HitCounterDemo.aspx.cs

using System;

public partial class HitCounterDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblHitCounter.Text = Application["NoOfUsers"].ToString();
}
}
Post your comment