I am trying to wirite a custom session provider in .net framework 4.8. What I do not understand, and my problem is that when I try to access data like
Session[key] I imagine that the session provider GetItem should got called, but that’s not happening.
Here is my session provider prototype where I was trying to do this:
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Web;
using System.Web.SessionState;
namespace MVC
{
public class PPMSesseionStateProvider : SessionStateStoreProviderBase
{
private string baseURl = string.Empty;
public override void Initialize(string name, NameValueCollection config)
{
if (String.IsNullOrEmpty(config["description"]))
{
config.Remove("description");
config.Add("description", "Custom PPM Session State Store provider");
}
baseURl = ConfigurationManager.AppSettings["baseURL"];
base.Initialize(name, config);
}
public override SessionStateStoreData CreateNewStoreData(HttpContext context, int timeout)
{
return new SessionStateStoreData(new SessionStateItemCollection(), SessionStateUtility.GetSessionStaticObjects(context), timeout);
}
public override void CreateUninitializedItem(HttpContext context, string id, int timeout)
{}
public override void Dispose()
{}
public override void EndRequest(HttpContext context)
{}
public override SessionStateStoreData GetItem(HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions)
{
//SAME AS GETITEM
var request = WebRequest.Create($"https://localhost:5000/SessionTest/get/asdfrgfds");
request.ContentType = "application/json; charset=utf-8";
request.Method = "GET";
request.Headers.Add("PPMSessionHeader", "12345");
var sessionResponse = (HttpWebResponse)request.GetResponse();
// Initial values for return value and out parameters.
SessionStateStoreData item = null;
lockAge = TimeSpan.Zero;
lockId = null;
locked = false;
actions = 0;
var response = string.Empty; //get the reposnse form the server (look for the object type in the server) and create
SessionStateItemCollection sessionItems = new SessionStateItemCollection();
if (sessionResponse.ContentLength > 0)
{
var reader = new BinaryReader(sessionResponse.GetResponseStream());
sessionItems = SessionStateItemCollection.Deserialize(reader);
}
return new SessionStateStoreData(sessionItems, SessionStateUtility.GetSessionStaticObjects(context), 1000);
}
public override SessionStateStoreData GetItemExclusive(HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions)
{
//SAME AS GETITEM
var request = WebRequest.Create($"https://localhost:5000/SessionTest/get/asdfrgfds");
request.ContentType = "application/json; charset=utf-8";
request.Method = "GET";
request.Headers.Add("PPMSessionHeader", "12345");
var sessionResponse = (HttpWebResponse)request.GetResponse();
// Initial values for return value and out parameters.
SessionStateStoreData item = null;
lockAge = TimeSpan.Zero;
lockId = null;
locked = false;
actions = 0;
var response = string.Empty; //get the reposnse form the server (look for the object type in the server) and create
SessionStateItemCollection sessionItems = new SessionStateItemCollection();
if (sessionResponse.ContentLength > 0)
{
var reader = new BinaryReader(sessionResponse.GetResponseStream());
sessionItems = SessionStateItemCollection.Deserialize(reader);
}
return new SessionStateStoreData(sessionItems, SessionStateUtility.GetSessionStaticObjects(context), 1000);
}
public override void InitializeRequest(HttpContext context)
{}
public override void ReleaseItemExclusive(HttpContext context, string id, object lockId)
{}
public override void RemoveItem(HttpContext context, string id, object lockId, SessionStateStoreData item)
{
//send request to remove item from session
}
public override void ResetItemTimeout(HttpContext context, string id)
{}
public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem)
{
//send the key and the value to the API
}
public override bool SetItemExpireCallback(SessionStateItemExpireCallback expireCallback) => false;
}
}
The controller action where I test this:
public ActionResult Index()
{
string key = "ProductID:1001";
var customObject = Session[key]; // at this line nothing is triggered !!!
Session.Add(key, "tralalalala");
return View();
}
thnx