ASP NET Core MVC - 如何配置进程外会话状态?

Mua*_*878 3 asp.net-core-mvc asp.net-core

有没有办法使用 ASP.NET Core MVC 配置进程外会话状态(使用 Windows 状态服务器或 SQL Server)?

Mar*_*cki 5

重要的是会话数据由缓存支持。您需要将 IDistributedCache 实现添加到您的应用程序服务,而不是内存缓存,即:

对于 SQL Server:

services.AddDistributedSqlServerCache(options =>
    {
        options.ConnectionString = @"your_connection_string";
        options.SchemaName = "dbo";
        options.TableName = "TestCache";
    });
Run Code Online (Sandbox Code Playgroud)

(确保您Microsoft.Extensions.Caching.SqlServer在项目中添加了包)

对于Redis:

services.AddDistributedRedisCache(options =>
    {
        options.Configuration = "localhost";
        options.InstanceName = "SampleInstance";
    });
Run Code Online (Sandbox Code Playgroud)

(包:Microsoft.Extensions.Caching.Redis

我不知道是否有针对 ASP.NET Core 分布式缓存的 Windows State Server 的实现。

分布式会话的文档链接:https : //docs.microsoft.com/en-us/aspnet/core/performance/caching/distributed

有关 ASP.NET Core 中的会话的更多信息:https : //docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state