In ASP.NET, there are four ways to store Session State. The way I prefer to store it is using SQL Server. However, instead of storing it in the ASPstate database, which is the default way to store it on SQL Server, I choose to store it in the application database, which is a “custom SQL database” implementation.
When storing Session State in the application database, the web.config will have an entry that points at the database server and the name of the database:
<sessionState mode="SQLServer" timeout="20" allowcustomsqldatabase="true"
sqlconnectionstring="Data Source=Server;Initial Catalog=databasename; User ID=UserID;
Password=Password;" cookieless="false" />
Because this is set in web.config, it suffers from the same problem that I wrote about in this post — namely, that the connection string must be changed when moving between different environments (such as development, test / staging , and production).
To remedy this, a function that returns the dynamic connection string for the application should already be in place. Something like this will work:
public class ConnectionStrings
public static string _AppDB_ConnectionString = ConfigurationManager.ConnectionStrings["AppDB_ConnectionString_" + ConfigurationManager.AppSettings.Get("Environment").ToUpper()].ConnectionString;
(This is assuming that you have an “Environment” key set in the appSettings section of either web.config or machine.config, and that your connection strings are named accordingly in your web.config connectionStrings section.)
Next, a PartitionResolver class must be created that can get the correct connection string by using the function declared above:
public class MyPartitionResolver : System.Web.IPartitionResolver
public void Initialize()
public string ResolvePartition(object key)
Lastly, the sessionState entry in web.config must be modified to look like this:
<sessionState mode="SQLServer" allowCustomSqlDatabase="true" partitionResolverType="MyPartitionResolver"
regenerateExpiredSessionId="true" compressionEnabled="true" useHostingIdentity="true" timeout="480"/>
Note: The options after “partitionResolverType” can be set according to your needs.