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 session.
Session
Session stores data for each web user. The data about a user is also stored server side, but it can only be retrieved with a user GUID, which is stored client side using a cookie or modified URL. If the client application does not store the GUID persistently, then when the client application terminates, this state data is lost.
We have three ways to store session data.
- InProc : The default way; session data is stored in memory within the IIS process. Such an application does not scale – all the following user requests must be sent to the same server where the session data is stored. But it is the fastest.
- StateServer : Session data is stored in memory by a Windows service separate from IIS. Can be on a different server. This way the application is scalable. The second fastest.
- SqlServer : Store the session data in SQL server. Slowest but most reliable.
The mode can be InProc, StateServer or SqlServer. stateConnectionString is the URL of the machine running the state server, and sqlConnectionString is only used when you choose SqlServer option for mode. Timeout is the life span of the session data.
<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
cookieless="false"
timeout="10"
/>
First we need a Web Service. I created a basic Web Service as.
Creating XML Web Service in .Net
Here is sample code I use to create and consume ASP.NET Web Services.
Example of Testing Web Service in .NetStep 1 : Create the 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 "MySessionEnabledWebService". Then click the OK Button. A screenshot of this activity is shown below.

Step 2 : Click on the "OK" button; you will see the following window.

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 "GetPerUserClickCount()" which returns an integer value.
Service.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace MySessionEnabledWebService
{
/// <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(EnableSession = true)]
public int GetPerUserClickCount()
{
int count;
if (Session["ConnectCount"] == null)
count = 1;
else
count = (int)Session["ConnectCount"] + 1;
Session["ConnectCount"] = count;
return count;
}
}
}
Note: The key thing to do is to add (EnableSession = True) to the WebMethod tag. The rest of the code simply keeps track of how many times each client has called the Web Service method.
Side Note: I am using a Nullable Type to simplify the retrieving of Count from the Session collection.
if (Session["ConnectCount"] == null)
count = 1;
else
count = (int)Session["ConnectCount"] + 1;
Session["ConnectCount"] = count;















Join the conversation! Your thoughts help the community grow.