C# asp.net Core-在Httpcontext.Session上获取system.InvalidOperationException

C# asp.net Core-在Httpcontext.Session上获取system.InvalidOperationException,c#,asp.net-core,C#,Asp.net Core,我已尝试根据和教程在我的asp.net核心项目上设置会话 设置会话的一部分是添加到启动中: services.AddDistributedMemoryCache() services.AddSession() 并在启动配置中: app.UseAuthentication(); app.UseConfigureSession(); // this.. app.UseStaticFiles(); ap

我已尝试根据和教程在我的asp.net核心项目上设置会话

设置会话的一部分是添加到启动中:

        services.AddDistributedMemoryCache()
        services.AddSession()
并在启动配置中:

        app.UseAuthentication();
        app.UseConfigureSession();  // this..
        app.UseStaticFiles();
        app.UseSession();           // this..
        app.UseMvc(routes =>
现在一切都好了,我想在中间件功能中使用它

    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);
    }
但在我开始设置会话之前,我发现我得到了一个System.InvalidOperationException-请参阅下面的图片,其中我使用了一个断点来检查httpContext

然后在继续之后,它消失在乙醚中


我不知道如何解决这个问题。。不确定导致此问题的设置有何问题。。希望有人能给我们一些启示。

中间件是什么?您是否配置了app.UseSession()在配置此中间件之前?