dotnet / AspNetCore.Docs

Documentation for ASP.NET Core
https://docs.microsoft.com/aspnet/core
Creative Commons Attribution 4.0 International
12.56k stars 25.31k forks source link

[BUG] Documentation in correct for session state For 3.1 #18651

Closed davidbuckleyni closed 4 years ago

davidbuckleyni commented 4 years ago

In the Following the static class helpers that have been given is actually wrong in .net core 3.1 the string elements have been removed.

I have .net core 3.1 selection in the drop down menu and the statements are incorrect.

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

It should be this but even then it complains and gives an error that it cannot convert from readonly type of byte.

    public static void Set<T>(this ISession session, string key, T value) {
        session.Set(key, JsonSerializer.Serialize(value));
    }

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

Error that is given is

Severity Code Description Project File Line Suppression State Error CS0411 The type arguments for method 'Helpers.Get(ISession, string)' cannot be inferred from the usage. Try specifying the type arguments explicitly. ElectricalSurvey.DAL D:\GitMaster\sybersystemslimited\ElectricalSurvey\ElectricalSurvey\ElectricalSurvey.DAL\Helpers\Helpers.cs 19 Active


Document Details

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

Rick-Anderson commented 4 years ago

@davidbuckleyni just tested the code and it works fine. The type must be serializable.