4

I am migrating to ASP.NET CORE 5.0 and have middleware set up however upon setting up this part of my project I have been hit with httpContext.Session.GetString coming up as an error. This use to work but it appears they have removed both .GetString and .SetString.

Here is my code for middleware.

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);
        }
    }

Its erroring on the following piece of code:

if (httpContext.Session.GetString("connectionString") == null) // Session needs to be set..

The error I am getting is:

'ISession' does not contain a definition for 'SetString' and no accessible extension method 'SetString' accepting a first argument of type 'ISession' could be found (are you missing a using directive or an assembly reference?)

I note with intellisense that GetString and SetString show but have question marks behind them..

GetString has questionmarks after it

So my question is, If I cant use GetString to access (or create) my var connectionstring in this example how do I check/access/create a variable eg "connectionstring" in httpContext.session now that these methods have been deprecated?

si2030
  • 3,895
  • 8
  • 38
  • 87
  • 2
    Does https://stackoverflow.com/a/44851122/34092 help? – mjwills Sep 21 '20 at 06:36
  • Those questions marks in intellisense mean that it picked up on that method, due to you calling it a bunch of times, but it not existing, be it because you missed and import or because it simply doesn't exist. The same thing also happens when you start using a variable without ever actually declaring it – MindSwipe Sep 21 '20 at 06:41
  • I'm guessing you're migrating from a version where the extensions assembly was directly referenced. That is assembly is now just part of the framework. – Jeremy Lakeman Sep 21 '20 at 06:51
  • Thanks for the direction however I did look up "Microsoft.AspNetCore.Http.Extensions" and its only at version 2.2.0 - Im working in 5.0. Also, I had an idea that its just picked up what I did rather than there being an actual method.. So given I am working in 5.0 rc (I am migrating yes) is there a way of doing this in this partucular version.. – si2030 Sep 21 '20 at 06:52

1 Answers1

6

Those extensions do not yet exist in net.core 5.0 Preview. If you go to the documentation it redirects to 3.1

The requested page is not available for ASP.NET Core 5.0 Preview. You have been redirected to the newest product version this page is available for.

Why not adding them yourself? They are just net core extensions@github

        public static void SetString(this ISession session, string key, string value)
        {
            session.Set(key, Encoding.UTF8.GetBytes(value));
        }

        public static string GetString(this ISession session, string key)
        {
            var data = session.Get(key);
            if (data == null)
            {
                return null;
            }
            return Encoding.UTF8.GetString(data);
        }
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • 2
    now you can install `Microsoft.AspNetCore.Http.Extensions` NuGet package https://stackoverflow.com/a/44851122/34092 – SzilardD Feb 01 '21 at 06:13