Aspnetcore.docs: App session and app state update to 3.1

Created on 16 Jul 2019  ·  17Comments  ·  Source: dotnet/AspNetCore.Docs

@dougbu has anything changed in 3.0 for app and session state - that we need to document?

19.3K PV


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

P1 doc-bug

All 17 comments

@guardrex I'm not aware of anything new or different. @mkArtakMSFT❔ @rynowak❔

I had problems accessing HttpContext.Session, I got a Error: an object reference is required
I fixed it adding

@inject IHttpContextAccessor HttpContextAccessor

And then calling the Session methods using:

HttpContextAccessor.HttpContext.Session.SetString(...

@mkArtakMSFT please assign someone to review 3.0 changes to App session and app state. Customer reports changes.

When I examine ((Microsoft.AspNetCore.Builder.ApplicationBuilder)app)._components.Select(comp => comp.Target) at the end of my Configure method, I see no entry for SessionMiddleware, so apparently some other middleware is now doing its job in ASP.NET Core 3.0.0. I'm not sure what, though. The docs should be updated to reflect this.

@JunTaoLuo who can review session state changes for 3.0? We're getting lots of issues that things have changed for 3.0
cc @mkArtakMSFT

@anurse who would be the best person to review this?

@Tratcher would probably know if anything changed in Session. I'm not aware of any significant changes.

Session hasn't changed in 3.0. The samples do need to be updated for endpoint routing though.

@jez9999 does your app call UseSession? Nothing should have changed here.

@jcmarcanotrias that sounds like a problem with HttpContext in your razor page, not Session.

@Rick-Anderson Related to this work :point_right: https://github.com/dotnet/aspnetcore/issues/18081#issuecomment-572209346

Ah yeah, I forgot this was already on the docket :). Cool. I'll capture the gist of that linked issue here and close the aspnetcore one since we're tracking updates to this doc already.

Related to this work 👉 dotnet/aspnetcore#18081 (comment)

We should update the brief section we have detailing storing arbitrary objects using JSON serialization by ensuring that a 3.1 version of aspnetcore/fundamentals/app-state/samples/2.x/SessionSample exists that uses System.Text.Json in SessionExtensions

Could I just ask if there is a code sample anywhere on how to replace the Newtonsoft.Json sample here for a System.Text.Json version now that is the default?

For example, this is the current sample:

```csharp
public static class SessionExtensions
{
public static void Set(this ISession session, string key, T value)
{
session.SetString(key, JsonConvert.SerializeObject(value));
}

    public static T Get<T>(this ISession session, string key)
    {
        var value = session.GetString(key);

        return value == null ? default(T) : 
            JsonConvert.DeserializeObject<T>(value);
    }
}

```

@tdykstra do we have any System.Text.Json samples like the preceding?

If you replace JsonConvert.SerializeObject(value)
with JsonSerializer.Serialize(value) and JsonConvert.DeserializeObject<T>(value) with JsonSerializer.Deserialize<T>(value) it should just work, depending on what types you're trying to store. See the How to Migrate from Newtonsoft.Json article.
cc @ahsonkhan

Thank you for replying So, this is what I ended up doing, not sure if this is correct or not but seems to work:

using System.Text.Json;
using Microsoft.AspNetCore.Http;

namespace Web.Extensions
{
    public static class SessionExtensions
    {
        public static void Set<T>(this ISession session, string key, T value)
        {
            session.SetString(key, JsonSerializer.Serialize(value));
        }

        public static T Get<T>(this ISession session, string key)
        {
            var value = session.GetString(key);
            return value == null ? default : JsonSerializer.Deserialize<T>(value);
        }
    }
}

Could I just ask if there is a code sample anywhere on how to replace the Newtonsoft.Json sample here for a System.Text.Json version now that is the default?

For example, this is the current sample:

    public static class SessionExtensions
    {
        public static void Set<T>(this ISession session, string key, T value)
        {
            session.SetString(key, JsonConvert.SerializeObject(value));
        }

        public static T Get<T>(this ISession session, string key)
        {
            var value = session.GetString(key);

            return value == null ? default(T) : 
                JsonConvert.DeserializeObject<T>(value);
        }
    }
Was this page helpful?
0 / 5 - 0 ratings