ASP NET Core MVC - How to configure Out of Process Session State?

雨燕双飞 提交于 2019-12-06 07:27:49

What is important is that session data is backed by a cache. You need to add IDistributedCache implementation to your application services, instead of the in-memory cache, i.e:

for SQL Server:

services.AddDistributedSqlServerCache(options =>
    {
        options.ConnectionString = @"your_connection_string";
        options.SchemaName = "dbo";
        options.TableName = "TestCache";
    });

(make sure that you have Microsoft.Extensions.Caching.SqlServer package added in your project)

for Redis:

services.AddDistributedRedisCache(options =>
    {
        options.Configuration = "localhost";
        options.InstanceName = "SampleInstance";
    });

(package: Microsoft.Extensions.Caching.Redis)

I don't know if there's an implementation for Windows State Server for ASP.NET Core distributed cache.

Documentation link for distributed session: https://docs.microsoft.com/en-us/aspnet/core/performance/caching/distributed

More about session in ASP.NET Core: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state

Thanks Marcin. I figured it out... but it was tricky getting it to work when targeting net46 instead of core (not all versions of Microsoft.Extensions.Caching.SqlConfig.Tools are supported by net46 and it wasn't apparent because my app wasn't throwing errors - just getting null values for the session variables I was setting...)

What finally worked for me was this configuration in project.json :

Under dependencies:

"Microsoft.AspNetCore.Session": "1.0.0",
"Microsoft.Extensions.Caching.SqlServer": "1.1.0-preview1-final",
"Microsoft.Extensions.Caching.SqlConfig": "1.0.0-rc1-final"

Under tools:

"Microsoft.Extensions.Caching.SqlConfig.Tools": "1.0.0-rc1-final"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!