asp.net-mvc - 如何在 .net 核心中模拟 SessionStateBehavior.ReadOnly
问题描述
我需要在 .net 核心中发出并行请求。
在 MVC 中很容易做到这一点
SessionStateBehavior.ReadOnly
但我想我们不能在 .net core 应用程序中使用,所以如何System.Web.SessionState.SessionStateBehavior.ReadOnly
在 .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;
推荐阅读
- c++ - 无法在 Mac OS X 上链接静态库
- php - Symfony 搜索,参数未发送
- c# - Best way to make a WPF application that will later become a library for a AutoCAD .net program
- java - Java多线程同步问题
- python - 如何使用 python 3.x 从远程 url 打印 csv 内容?
- java - How to access for loop variables outside for loop?
- here-api - HERE Transit APi (modes)
- visual-studio-code - 显示快速建议,但不要自动插入
- php - 重定向到链接然后重定向到codeigniter中的主网站?
- mysql - MySQL 多左连接限制