SIGN UP MEMBER LOGIN:    
ARTICLE

Application State Management in Web Service

Posted by Alok Pandey Articles | Web Services in C# December 05, 2011
Application is used to manage the state at server side. In this article, you will learn to use application state management in a web service.
Reader Level:

Introduction:

Application state management is server side state management. It is stored on the memory of server.

Here we will create a web service using Application state management. This service will return the number that how many times the service has  accessed. Let's create web service. Follow the given steps.

  • Go to Visual Studeo 2010  and take a New Project.
  • Take a ASP.NET Web Service Application.
  • Click ok button.
  • Replace the code with the following code.


using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.Services;
 

namespace
EHWS
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 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 Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public int data()
        {
            if (Application["
appli"] == null)
            {
                Application["
appli"] = 1;
            }
            else
            {
                int i = (int)Application["
appli"];
                i++;
                Application["
appli"] = i;
            }
            return (int)Application["
appli"];
        }
   }
}

Run the service.

Output:

1.gif

After creating service, create a web application and write the following code on .aspx.cs page.

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

namespace
aews
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
        protected void btnok_Click1(object sender, EventArgs e)
        {
            localhost.Service1 ob = new localhost.Service1();
            TextBox1.Text = ob.data().ToString();
        }
    }
}

 
Run the web application.

Output:

2.gif


Click the "ok" button.

Output:

3.gif


Copy the URL of your running web application and Take a new Tab of your browser and paste it. Now click at "ok" button.

Output:

4.gif

Means when we access the service by calling method, it will show how many times it has been accessed. 

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor