I’m developing an asp.net core application and using the Singleton Pattern for Session management, but when I want to read the values, it displays the null value !!
Below you can see the sample code.
SessionSingleton.cs
<code>[Serializable]
public sealed class SessionSingleton : IDisposable
{
#region Private Members
private const string SessionSingletonName = "SessionKey_502E69E5-668B-E011-951F-00155DF26207";
private static readonly object LockObject = new();
#endregion
#region Singleton
private SessionSingleton()
{
}
public static SessionSingleton Current
{
get
{
lock (LockObject)
{
string? value;
SessionSingleton sessionSingleton;
var session = CustomHttpContext.Current.Session;
if (session == null)
return null;
if (session.GetString(SessionSingletonName) is not null)
{
value = session.GetString(SessionSingletonName);
sessionSingleton = JsonSerializer.Deserialize<SessionSingleton>(value!)!;
}
else
{
sessionSingleton = new SessionSingleton();
var sessionText = JsonSerializer.Serialize(sessionSingleton);
session.SetString(SessionSingletonName, sessionText);
}
return sessionSingleton;
}
}
}
#endregion
#region Public Propertices
public string Name { get; set; }
#endregion
#region Dctor
public void Dispose()
{
// HttpContext.Current.Session[SessionSingletonName] = null;
// HttpContext.Current.Session.Remove(SessionSingletonName);
}
#endregion
}
</code>
<code>[Serializable]
public sealed class SessionSingleton : IDisposable
{
#region Private Members
private const string SessionSingletonName = "SessionKey_502E69E5-668B-E011-951F-00155DF26207";
private static readonly object LockObject = new();
#endregion
#region Singleton
private SessionSingleton()
{
}
public static SessionSingleton Current
{
get
{
lock (LockObject)
{
string? value;
SessionSingleton sessionSingleton;
var session = CustomHttpContext.Current.Session;
if (session == null)
return null;
if (session.GetString(SessionSingletonName) is not null)
{
value = session.GetString(SessionSingletonName);
sessionSingleton = JsonSerializer.Deserialize<SessionSingleton>(value!)!;
}
else
{
sessionSingleton = new SessionSingleton();
var sessionText = JsonSerializer.Serialize(sessionSingleton);
session.SetString(SessionSingletonName, sessionText);
}
return sessionSingleton;
}
}
}
#endregion
#region Public Propertices
public string Name { get; set; }
#endregion
#region Dctor
public void Dispose()
{
// HttpContext.Current.Session[SessionSingletonName] = null;
// HttpContext.Current.Session.Remove(SessionSingletonName);
}
#endregion
}
</code>
[Serializable]
public sealed class SessionSingleton : IDisposable
{
#region Private Members
private const string SessionSingletonName = "SessionKey_502E69E5-668B-E011-951F-00155DF26207";
private static readonly object LockObject = new();
#endregion
#region Singleton
private SessionSingleton()
{
}
public static SessionSingleton Current
{
get
{
lock (LockObject)
{
string? value;
SessionSingleton sessionSingleton;
var session = CustomHttpContext.Current.Session;
if (session == null)
return null;
if (session.GetString(SessionSingletonName) is not null)
{
value = session.GetString(SessionSingletonName);
sessionSingleton = JsonSerializer.Deserialize<SessionSingleton>(value!)!;
}
else
{
sessionSingleton = new SessionSingleton();
var sessionText = JsonSerializer.Serialize(sessionSingleton);
session.SetString(SessionSingletonName, sessionText);
}
return sessionSingleton;
}
}
}
#endregion
#region Public Propertices
public string Name { get; set; }
#endregion
#region Dctor
public void Dispose()
{
// HttpContext.Current.Session[SessionSingletonName] = null;
// HttpContext.Current.Session.Remove(SessionSingletonName);
}
#endregion
}
CustomHttpContext
<code> public static class CustomHttpContext
{
private static IHttpContextAccessor _httpContextAccessor = new HttpContextAccessor();
public static HttpContext? Current
{
get
{
if (_httpContextAccessor == null)
_httpContextAccessor = new HttpContextAccessor();
return _httpContextAccessor.HttpContext;
}
}
}
</code>
<code> public static class CustomHttpContext
{
private static IHttpContextAccessor _httpContextAccessor = new HttpContextAccessor();
public static HttpContext? Current
{
get
{
if (_httpContextAccessor == null)
_httpContextAccessor = new HttpContextAccessor();
return _httpContextAccessor.HttpContext;
}
}
}
</code>
public static class CustomHttpContext
{
private static IHttpContextAccessor _httpContextAccessor = new HttpContextAccessor();
public static HttpContext? Current
{
get
{
if (_httpContextAccessor == null)
_httpContextAccessor = new HttpContextAccessor();
return _httpContextAccessor.HttpContext;
}
}
}
HomeController
<code> public partial class HomeController : BaseController
{
public IActionResult Index()
{
SessionSingleton.Current.Name = "Test";
var value = SessionSingleton.Current.Name; // value is always null
SessionSingleton.Current.Name = "Test2";
value = SessionSingleton.Current.Name;// value is always null
return Ok(true);
}
}
</code>
<code> public partial class HomeController : BaseController
{
public IActionResult Index()
{
SessionSingleton.Current.Name = "Test";
var value = SessionSingleton.Current.Name; // value is always null
SessionSingleton.Current.Name = "Test2";
value = SessionSingleton.Current.Name;// value is always null
return Ok(true);
}
}
</code>
public partial class HomeController : BaseController
{
public IActionResult Index()
{
SessionSingleton.Current.Name = "Test";
var value = SessionSingleton.Current.Name; // value is always null
SessionSingleton.Current.Name = "Test2";
value = SessionSingleton.Current.Name;// value is always null
return Ok(true);
}
}
All code like serialization and Deserialization works fine, but Why is value always null in HomeController?