1

In Visual Studio 2015 RC, ASP.NET 5 vNext, i am trying to use SQL Session State in MVC 6. I'm not sure how to go about it. Can anyone show me a sample or give me some tips?

I'm just trying out Azure Load Balancer. I set up a load balanced set and linked two VM's to it. It's working as expected as I can see the machine name changing between the two VM's, when I go to a view that has the following code. @System.Environment.MachineName

I tried enabling session as shown in this link How to implement Session State in ASP.NET vNext MVC 6. but since this session is kept in the server, when the user is moved to a different server in the load balanced set, the session state does not persist between servers.

I know you can persist the same session if SQL Server Session State is used. In ASP.NET 4, it can be done easily just by changing sessionState element in web.config as follows.

<sessionState 
            mode="SQLServer"
            sqlConnectionString="data source=127.0.0.1;user id=<username>;password=<strongpassword>"
            cookieless="false" 
            timeout="20" 
    />

I'm hoping anyone can show me how this can be done in ASP.NET 5 (vnext)

Community
  • 1
  • 1

1 Answers1

0

Support for SQL Server Session State is currently in development. So your best options for now are using In-Memory session(which has the issue which you mentioned about state not persisted for multiple servers behind load balancer kind of scenarios). However you could try using Redis cache(ASP.NET 5 Session implementation is dependent on IDistributedCache based implementations of which there is an implementation for Redis cache).

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<IDistributedCache, RedisCache>();
    services.AddSession();

//-----------

public void Configure(IApplicationBuilder app)
{
   app.UseSession();

You need to reference following packages:

Microsoft.Framework.Caching.Redis
Microsoft.AspNet.Session
Kiran
  • 56,921
  • 15
  • 176
  • 161
  • Thank you for the information. I thought RC meant it was almost all ready :) Do you know the timeline for Microsoft to add SQL Server Session State? Am I just too naive/optimistic trying to use ASP.NET for production purpose? – Parallel Park Jun 15 '15 at 18:09
  • RC is actually for Visual Studio where as ASP.NET 5 is still in Beta. I know this is confusing but that is how it is as of now. The SQL Server Session state implementation should be available in next Beta but I cannot promise a timeline though :-) – Kiran Jun 15 '15 at 18:33