首页 > 解决方案 > 如何在 .net 核心中模拟 SessionStateBehavior.ReadOnly

问题描述

我需要在 .net 核心中发出并行请求。

在 MVC 中很容易做到这一点

SessionStateBehavior.ReadOnly

但我想我们不能在 .net core 应用程序中使用,所以如何System.Web.SessionState.SessionStateBehavior.ReadOnly在 .net core 中进行模拟

标签: asp.net-mvcasp.net-core.net-core

解决方案


我想我明白了。

我们不能使用SessionStateBehavior.ReadOnly,但是有一种新的方法可以进行并行请求。通常会话锁定请求队列,我们​​必须等待请求完成并处理到下一个请求表单队列。

但是,如果我们使 Session 加载与Session.LoadAsync()方法异步而不是使请求并行。

我们是如何做到的

internal static async Task<int> GetSessionInt(this ISession session, string key)
        {
            await session.LoadAsync();
            return session.GetInt32(key).Value;
        }

比从行动中调用方法

如果动作是异步的

int id = await HttpContext.Session.GetSessionInt("id");

如果动作不是异步的

int id = HttpContext.Session.GetSessionInt("id").Result;

推荐阅读