Writing to a session variable with SessionStateBehavior.ReadOnly
asp.net, asp.net-mvc, c#, http, session
Solution
If you target .NET Framework 4.6.2 and you're on SQL Server, you could leverage the new async SessionState module to access the session storage providers asynchronously.
Download and install the SessionStateModule and the SqlSessionState NuGet packages.
Consider also that SQLServer mode stores session state in the SQL Server database.
Note (from the comments)
While the session in ASP.NET Core is non-locking, only next release of `SqlSessionStateProviderAsync` should introduce this feature, according to this msdn blog.
Alternative provider
Another, different option would be to use StackExchange.Redis: e.g. for a web app in Azure App Service, follow these configuration steps. More generally, in a Redis server or servers, RedisSessionProvider never locks the Session
Problem
In my ASP.NET MVC 5 application, I'm performing a `GET` request on a method inside a controller that needs to read a value stored in session. To avoid session state locking issue, I've set `SessionStateBehavior` to `ReadOnly` on the class level. ``` [SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)] public class TestController: Controller { var test = Session["KEY"]; ... ``` However, very occasionally, I need to overwrite the `Session` variable to something else inside that same method. ASP.NET MVC does not allow me to do this with `SessionStateBehavior` set to `ReadOnly`. I can't set it to `Required` because then I run into the issue of session state locking issue again, preventing concurrent AJAX requests. What's a good solution for this? Edit: We're using SQL server for session state management.