c# - asp.net Core - Getting a system.InvalidOperationException on Httpcontext.Session
问题描述
I have attempted to set up a session on my asp.net core project as per this and this tutorial(s).
Part of setting up Session was to add into startup:
services.AddDistributedMemoryCache()
services.AddSession()
and in startup configure:
app.UseAuthentication();
app.UseConfigureSession(); // this..
app.UseStaticFiles();
app.UseSession(); // this..
app.UseMvc(routes =>
All good now I wanted to use it in a middleware function..
public async Task InvokeAsync(HttpContext httpContext, IUserSession userSession, ISessionServices sessionServices)
{
if (httpContext.User.Identities.Any(id => id.IsAuthenticated))
{
if(httpContext.Session.GetString("connectionString") == null) // Session needs to be set..
{
userSession.userId = httpContext.User.Claims.FirstOrDefault(x => x.Type == "userId")?.Value;
userSession.connectionString = sessionServices.ConnectionStringfromUserId(userSession.userId);
httpContext.Session.SetString("userId", userSession.userId);
httpContext.Session.SetString("connectionString", userSession.connectionString);
}
else // Session set so all we need to is to build userSession for data access..
{
userSession.userId = httpContext.Session.GetString("userId");
userSession.connectionString = httpContext.Session.GetString("connectionString");
}
}
// Call the next delegate/middleware in the pipeline
await _next.Invoke(httpContext);
}
But before I even get to setting Session I found I was getting a System.InvalidOperationException - see pic below where I have used a break point to examine httpContext..
It then disappears into the ether after continuing..
Not sure how to fix this.. not sure what is wrong with the setup to cause this.. hoping someone might shed some light.
解决方案
推荐阅读
- r - 如何在 R 的 Highchart 瀑布图中添加“isSum”栏?
- reactjs - 通过 axios 发送多请求,如果第一次响应 500 则停止所有请求并重定向到登录
- c - 无法为任何操作打开 FIFO
- abap - 如何为功能模块构建调用图?
- uipath - 如何使用 UIPath 在 Web 应用程序上查找损坏的链接?
- wikipedia - Wikipedia iwlinks 表仅存储一些指向 Wikidata 页面的链接。其他人在哪里?
- php - 扫描目录,使用从 1 开始的新键创建 php 数组,并随着找到的每个文件上升(我所有的键都是 [1])
- kubernetes - Kubernetes - NodePort 连接问题
- python - Ansible:对树结构进行交互
- python - 您如何处理代码中的不同 KeyError