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

namespace MotleyFlash.AspNetCore.MessageProviders
{
    public class SessionMessageProvider : IMessageProvider
    {
        public SessionMessageProvider(ISession session)
        {
            this.session = session;
        }

        private static JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings()
        {
            TypeNameHandling = TypeNameHandling.All
        };

        private readonly ISession session;
        private const string sessionKey = "motleyflash-session-message-provider";

        public object Get()
        {
            byte[] value;
            object messages = null;

            if (session.TryGetValue(sessionKey, out value))
            {
                if (value != null)
                {
                    messages =
                        JsonConvert.DeserializeObject(
                            Encoding.UTF8.GetString(value, 0, value.Length),
                            jsonSerializerSettings);
                }
            }

            return messages;
        }

        public void Set(object messages)
        {
            session.Set(
                sessionKey,
                Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(
                    messages,
                    jsonSerializerSettings)));

            session.CommitAsync();
        }
    }
}

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

namespace MotleyFlash.AspNetCore.MessageProviders
{
    public class SessionMessageProvider : IMessageProvider
    {
        public SessionMessageProvider(ISession session)
        {
            this.session = session;
        }

        private static JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings()
        {
            TypeNameHandling = TypeNameHandling.All
        };

        private readonly ISession session;
        private const string sessionKey = "motleyflash-session-message-provider";

        public object Get()
        {
            byte[] value;
            object messages = null;

            if (session.TryGetValue(sessionKey, out value))
            {
                if (value != null)
                {
                    messages =
                        JsonConvert.DeserializeObject(
                            Encoding.UTF8.GetString(value, 0, value.Length),
                            jsonSerializerSettings);
                }
            }

            return messages;
        }

        public void Set(object messages)
        {
            session.Set(
                sessionKey,
                Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(
                    messages,
                    jsonSerializerSettings)));
        }
    }
}

Remove session-commit statement that is not needed and already handled by middleware
Remove session-commit statement that is not needed and already handled by middleware

Ref. https://github.com/aspnet/Session/blob/dabd28a5d9ac7837afbfce7c8dfa2805a9559857/src/Microsoft.AspNetCore.Session/SessionMiddleware.cs\#L116.
