【发布时间】:2020-09-21 16:35:44
【问题描述】:
我正在迁移到 ASP.NET CORE 5.0 并设置了中间件,但是在设置我的项目的这一部分时,我遇到了 httpContext.Session.GetString 出现错误。这可以工作,但似乎他们已经删除了 .GetString 和 .SetString。
这是我的中间件代码。
public class ConfigureSessionMiddleware
{
private readonly RequestDelegate _next;
public ConfigureSessionMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext httpContext, IUserSession userSession, ISessionServices sessionServices)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (userSession == null)
{
throw new ArgumentNullException(nameof(userSession));
}
if (sessionServices == null)
{
throw new ArgumentNullException(nameof(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).ConfigureAwait(false);
}
}
以下代码出错:
if (httpContext.Session.GetString("connectionString") == null) // Session needs to be set..
我得到的错误是:
“ISession”不包含“SetString”的定义,并且没有 可访问的扩展方法“SetString”接受第一个参数 可以找到类型“ISession”(您是否缺少 using 指令或 程序集参考?)
我注意到 GetString 和 SetString 显示但后面有问号..
所以我的问题是,如果我不能在这个例子中使用GetString 来访问(或创建)我的 var 连接字符串,我该如何检查/访问/创建一个变量,例如 httpContext.session 中的“连接字符串”,因为这些方法有被弃用了吗?
【问题讨论】:
-
智能感知中的那些问号意味着它采用了该方法,因为您多次调用它,但它不存在,无论是因为您错过并导入还是因为它根本不存在不存在。当您开始使用变量而没有实际声明它时,也会发生同样的事情
-
我猜您正在从直接引用扩展程序集的版本迁移。那就是汇编现在只是框架的一部分。
-
感谢您的指导,但是我确实查找了“Microsoft.AspNetCore.Http.Extensions”,并且它仅在 2.2.0 版本中 - 我在 5.0 中工作。另外,我有一个想法,它只是拿起了我所做的,而不是有一个实际的方法。所以鉴于我在 5.0 rc 中工作(我正在迁移是的),有没有办法在这个特定版本中做到这一点。
标签: c# asp.net-core