Introducing the ASP.Net Async SessionState Module
SessionStateModule is ASP.NET’s default session-state handler which retrieves session data and writes it to the session-state store. It already operates asynchronously when acquiring the request state, but it doesn’t support async read/write to the session-state store. In the .NET Framework 4.6.2 release, we introduced a new interface named ISessionStateModule to enable this scenario.
Benefits of the asynchronous SessionState module
It’s all about the scalability. As the world moves to the cloud, it makes really easy to scale-out computing resources to serve the large spikes in service requests to an application. It’s very important to design a scalable system so that an application benefits from cloud computing architecture. When you are considering the scalability in terms of session-state, you should not use in-memory session-state provider. The in-memory provider makes it impossible to share session data across multiple web servers. You need to store session data in other mediums such as Microsoft Azure SQL Database, NoSQL, Azure Redis Cache etc. In this case, the new async Sessionsate module enables you to plug in the async session-state provider to access those storage providers asynchronously. Async I/O operation helps release the thread more quickly than synchronous I/O operation, and ASP.NET can handle other requests. If you are interested in knowing more details about async programming, you can read Stephen Cleary’s article on Async Programming : Introduction to Async/Await on ASP.NET.
How to use the Async SessionState module
- Open the NuGet package manager and search for Microsoft.AspNet.SessionState.SessionStateModule and install. Since the ISessionStateModule interface is introduced in .NET Framework 4.6.2, you need to target your application to .NET Framework 4.6.2. Download the .NET Framework 4.6.2 Developer Pack if you do not already have it installed.
- The NuGet package will copy Microsoft.AspNet.SessionState.SessionStateModule.dll to the bin folder and add the following configuration into the web.config file. If you don’t provide an async session-state provider, the module will use a default in-memory provider. With the in-memory provider you won’t get the async benefits from the default provider, since the default provider just stores the session data in memory.
- If you want to get all the async benefits mentioned above, you need to install a real async sessionstate provider. With the release of Microsoft.AspNet.SessionState.SessionStateModule NuGet package, we are also releasing an async version Sql sessionstate provider NuGet package which leverages Entity Framework to do the async database operation. To install this package, open NuGet package manager and search for the Microsoft.AspNet.SessionState.SqlSessionStateProviderasync package and install it.
- The installation will add Microsoft.AspNet.SessionState.SqlSessionStateProviderAsync.dll to the bin folder and insert the following configuration into the web.config file. The only additional configuration you need to do is to define a connection string within the connectionstrings element with the same name as the value in the connectionStringName attribute. In the sample configuration below, my connection string would be “DefaultConnection”.
How to implement an async provider
In most of the cases, you can leverage the Microsoft.AspNet.SessionState.SessionStateModule NuGet package and just implement the provider if you want to store the session data somewhere else. To implement your own async sessionstate provider which works with Microsoft.AspNet.SessionState.SessionStateModule, all you need to do is to implement a concrete SessionStateStoreProviderAsyncBase class which is included in the Microsoft.AspNet.SessionState.SessionStateModule NuGet package. Here is the signature for that class:
The new async session-state provider base class is almost same as the sync version, except the majority of the methods return a task in async sessionstate provider base class. You can reference the
sample code of sync version sessionstate provider and use async version System.Data.Odbc API to do the database operation instead. We think that this will assist in some performance tuning scenarios with ASP.NET, and encourage you to try this provider if you are currently using SQL Server as your session state provider. Let us know if you are building an async provider for another storage medium. We encourage you to share any new sessionstate providers you write on NuGet.org. Good luck and happy coding!