Web Services State Management Using Application Object

Introduction

 
In ASP.NET, there are two places in which we can store state information.
  • Application 
  • Session
In this article, I am going to describe only the applications way. For the description of the session, way read my last article Using Session State in a Web Service.
 
XML Web services can use Application objects for managing the state. The Application object can be used as a shared container for state management. The Application object will allow us to store the variables or object references that are available to all the visitors to the XML Web service for the lifetime of the web service. The Application object is "in-process" which implies that it can run in the same process as ASP.NET.
 
Application 
 
The Application object provides a mechanism for storing data that is accessible to all code running within the Web application. The application stores data for the server application. The data is stored in memory all through the lifetime of the server application (not the server), starting from the first page being requested. Each server application has its own Application. If the server application terminates, this Application data is lost.
 
One difference to be noted between Session and Application is that the Application object does not require the EnableSessionProperty to be set in the webmethod attribute. The Application is enabled by default for web services.
 
For example, let us examine the following code. We do not need to set the EnableSessionProperty = True as we did in the Session object. Because any class deriving from the Web service will have automatic access to the application object.
  1. [WebMethod]  
  2.       public int GetTotalClickCount()  
  3.       {  
  4.           int count;  
  5.           if (Application["ConnectCount"] == null)  
  6.               count = 1;  
  7.           else  
  8.               count = (int)Application["ConnectCount"] + 1;  
  9.           Application["ConnectCount"] = count;  
  10.           return count;  
  11.       } 
To implement the above code first we need a Web Service. I created a basic Web Service.
 
Creating an XML Web Service in .Net
 
Here is the sample code I use to create and consume ASP.NET Web Services.
 
Example of Testing Web Service in .Net
 
Step 1: Create an ASP.NET. Web Service Source File.
 
Open Visual Studio 2010 and create a new web site.->Select .Net Framework 3.5. ->Select ASP.NET Web Service page -> Then, you have to give the name of your service. In this example, I am giving it the name "MyApplicationWebService". Then click the OK button. A screenshot of this activity is shown below.
 
img1.gif
 
Step 2: Click on the "OK" button; you will see the following window.
 
img2.gif
 
Here (in the above figure), you will note that there is a predefined method "HelloWorld" which returns the string "Hello World". You can use your own method and can perform other operations.
 
Here I made a simple method "GetTotalClickCount()" which returns an integer value.
 
Service.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Services;  
  6. namespace MyApplicationWebService  
  7. {  
  8.    /// <summary>  
  9.     /// Summary description for Service1  
  10.     /// </summary>  
  11.     [WebService(Namespace = "http://tempuri.org/")]  
  12.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  13.     [System.ComponentModel.ToolboxItem(false)]  
  14.     // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.  
  15.     // [System.Web.Script.Services.ScriptService]  
  16.     public class Service1 : System.Web.Services.WebService  
  17.     {  
  18.         [WebMethod]  
  19.         public int GetTotalClickCount()  
  20.         {  
  21.             int count;  
  22.             if (Application["ConnectCount"] == null)  
  23.                 count = 1;  
  24.             else  
  25.                 count = (int)Application["ConnectCount"] + 1;  
  26.             Application["ConnectCount"] = count;  
  27.             return count;  
  28.         }  
  29.     }  
Step 3: Build the Web Service and Run the Web Service for testing by pressing the F5 function key.
 
img3.gif
 
Copy the URL of this web service for further use.
 
Step 4: Click on the "GetPerUserClickCount" Button to test the web service.
 
img4.gif
 
Click on the Invoke button to test the Web Service.
 
img5.gif
 
Refresh the browser or run the web service again and again. We will then get:
 
img6.gif
img7.gif
 
and so on 4,5,6,7...
 
Now our web service is ready to use. We just need to create a new web site to consume the web service.
 

Example of Testing Web Service in .Net

 
Step 5: Create a Test Web Site by File > New > Web Site > ASP.Net Web Site.
 
img8.gif
 
Give a name to the website; for example, here I have chosen the name "MyTestWebApplication" and clicked on the "ok" button.
 
Step 6: Right-click in the Solution Explorer and choose "Add Web Reference".
 
img9.gif
 
Step 7: Past the URL of the web service and click on the "Green arrow" button and then "Add reference".
 
img10.gif
 
Step 8: Now your web service is ready for use. You can see it in Solution Explorer.
 
img11.gif
 
Step 9: Go to the design of the Default.aspx page; drag and drop one TextBox and one Button.
 
img12.gif
 
Rename the Button as PressMe.
 
Step 10: Go to the Default.cs page and on the button click event using the following code.
  1. protected void Button1_Click(object sender, EventArgs e)  
  2.   
  3. {  
  4.         localhost.Service1 myservice = new localhost.Service1();  
  5.         TextBox1.Text=myservice.GetTotalClickCount().ToString();  
Step 11: Pressing the F5 function key to run the website, you will see.
 
img13.gif
 
Hit the PressMe.
 
img14.gif
 
See the output which starts from 4 because we already access the page 3 times see step 4.
 
img15.gif
 
img16.gif
 
Resources


Similar Articles