To overcome this limitation of the traditional Web programming, ASP.NET includes several options that help you preserve data on both a client side as well as server side.
These techniques are commonly known as state Management.
As per my understanding "State management is nothing but a temporary memory storage to store state of a page or imformation of a page against various Postback".
In ASP.NET, there are two types of state management techniques: Client side and Server side but in Web Service, we can have very limited state management techniques. Here in Web Services, we have only Server side State Management.
We have mainly three options of state management in the Web Services, which are shown below.
Hence, Session provides a facility to store the information on the Server memory. It can support any type of object to store along with our own custom objects. For every client, Session data is stored separately, which means Session data is stored on a per client basis.
For each session we have a unique block of memory allocated in the server. Session object is per client, which mean that different clients generate different Session objects.
To use ASP.NET Session object in a Web Service, there are two things, which we need to do which are shown below.
- The WebService class should inherit from System.Web.Services.WebService class.
- The EnableSession property of the WebMethod attribute should be set to true.
Hence, to work with Session state, let's create an empty ASP.NET project, as shown below.
Now, the second step is to add a Web Service file (.asmx) to the project, as shown below.
Now to work with Session, create a new method first and decorate it with the attribute [webMethod], as shown below.
Here, I will create a method, which will accept some product name and store it in Session, as shown below.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Services;
-
-
-
-
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
-
-
- public class WebService : System.Web.Services.WebService
- {
-
- public WebService()
- {
-
-
-
- }
-
- [WebMethod(EnableSession = true)]
- public string saveName(string pname)
- {
- List<string> li;
- if (Session["Name"] == null)
- {
- Session["Name"] = pname;
- return "Data saved successfully.";
-
- }
-
- else
- {
- Session["Name"] = Session["Name"] + "," + pname;
- return "Data saved successfully.";
- }
-
-
- }
- }
Here, I have described each line of the code, as shown below.
Now, we will create one more method to read all the products stored in the Session.
- [WebMethod(EnableSession = true)]
- public List<string> Display()
- {
- List<string> li1 = new List<string>();
- if (Session["Name"] == null)
- {
-
- li1.Add("No record to display");
- return li1;
- }
-
- else
- {
- string[] names = Session["Name"].ToString().Split(',');
- foreach (string s in names)
- {
- li1.Add(s);
- }
-
- return li1;
- }
-
- }
Now, in this method, we are splitting all the data stored in the Session and showing each data, using foreach loop.
Here, the two Webservice methods are shown below.
Now, just click on saveName and insert some product name, as shown below.
After adding each product, please click Invoke button to store these products in the Session.
Now, we want to retrieve these products from the Session, so we will click Display Service, as shown below and press invoke button.
In this way, we can use a Session in the Web Service.
Application Object
- Web Server allocates a block of memory, which is common to all the users or clients, called an Application object.
- Application object does not timeout.
- The Application object provides a mechanism to store the data and access the data throughout the Application.
By default, in the Web Service, the Application is Enabled, there is no need to enable it like Session.
Thus, let's try to work with the same example with an Application object in the Web Service. Here, I am creating the Web Method to save the data in an Application state, as shown below.
- [WebMethod]
- public string saveName(string pname)
- {
- List<string> li;
- if (Application["Name"] == null)
- {
- Application["Name"] = pname;
- return "Data saved successfully.";
-
- }
-
- else
- {
- Application["Name"] = Application["Name"] + "," + pname;
- return "Data saved successfully.";
- }
-
-
- }
Now, here is the Web method to retrieve the data from an Application.
- [WebMethod]
- public List<string> Display()
- {
- List<string> li1 = new List<string>();
- if (Application["Name"] == null)
- {
-
- li1.Add("No record to display");
- return li1;
- }
-
- else
- {
- string[] names = Application["Name"].ToString().Split(',');
- foreach (string s in names)
- {
- li1.Add(s);
- }
-
- return li1;
- }
-
- }
Here, we have used Application object in place of session. Please check it.
Thus, this Application data will appear until your WebServer is running because it does not have any timeout. Thus, it is mainly recommended to use when saving the data which is used .
Application object maintains its data till the Web Application is shut down.
CACHING IN Web Service
- It is a concept of storing frequently used data in local memory for faster access.This will reduce the time to regenerate the data once again.
- It is mainly used to improve the performance of the Application.
- If you want to cache a Web method in Webservice, we have a option of using "CacheDuration" to cache the content.
- In cacheDuration attribute, we have to mention the duration in number of seconds for which we want to cache the content.
- [WebMethod(CacheDuration =60)]
- public List<int> Display()
- {
- List<int> li1 = new List<int>();
-
-
- for(int i=0;i<10000;i++)
- {
- li1.Add(i);
- }
-
- return li1;
- }
Here is a simple Web method to have cache duration of 60 seconds in the WebService.
For more knowledge on caching in the Web Service, please click
here. I hope you liked this article. If you know any new state management techniques, please update me, so that I will include it in the article.