This topic is locked

Storing session data in SQL Server database

6/4/2022 2:18:43 AM
ASPRunner.NET Tips and tricks
admin

Build 39268 of ASPRunner.NET adds a new useful feature, an option to store session data in SQL Server database. This can be useful when your web application setup involves a load balancer and individual requests can be served by different web servers. Storing user's session in SQL Server is a must in this scenario to ensure smooth user experience.

There is an excellent article that talks about load balancers. We copied the most relevant parts here.

The "Web.config" changes are as in the following:

<sessionState mode="SQLServer" allowcustomsqldatabase="true" sqlconnectionstring="Data Source=<SERVERNAME>;Initial Catalog=<DATABASENAME>;User ID=<USERID>;Password=<PASSWORD>;" cookieless="false" timeout="100"/>

The benefit of using this technique is that all the data in the session will be stored together in a different location or you can say a centralized location in SQL Server, to get it working we just need to configure the SQLServer to store session data.

While configuring this mode in your application, one needs to consider that all the objects to be stored in a session should be serializable. So if you are currently using an In-Proc session state mode and intend that your application may require to have heavy sessions and you eventually need to shift it to SQLServer or the StateServer, then please take into consideration this "Serialization" issue from now on, because once the application is running and is the production environment, sometimes it is very difficult to make changes to session mode, because of these issues.

Configuring SQLServer to store Session data

Here I will show you how to configure SQLServer for storing session data in a custom database, although we also configure it to save session data in a temporary database, but this database goes off when you restart your SQL Server.
It is very simple by using the command line:

aspnet_regsql -d <SQLDATABASENAME> -S <SQLSERVERNAME> -U <USERNAME> -P <PASSWORD> -ssadd -sstype c

In this command "–d" specifies custom database name, "-S" specifies SQL Server Name, "-U" specifies Username, "-P" specifies Password, "-ssadd" is an option specifying we need to add support for handling session in SQLServer and "-sstype" specifies the type of storage for Session State (if we need a temporary database or a custom database) and "c" represents the custom database.

img alt

As soon as you are done with it, you will get a message saying "Finished" with a notification for the settings you need to do in "Web.Config". So here we can see the message saying to configure the sessionState in "Web.Config" to allow session data to be stored in a custom database and we need to specify the connection string for it. You can also check the database created by accessing that server; it will look something like this:

img alt

With this we are done with configuring Session State in SQLServer mode.

W
wshoapDevClub member 6/6/2022

Thank you. Do you know if this will work for Azure SQL Db and Azure App Service hosted app. My ASPNETRUNNER app is hosting in Azure

admin 6/7/2022

No personal experienc with this functionality and Azure but I don't see why won't it work. Give it a try.