SessionHelper.SetValue failing

Max De Los Reyes asked on August 18, 2023 18:26

As the title says, and similar to this question in the way that I am setting an object using the SessionHelper.SetValue method, it is returning null when I try to access it. The codefile is located within the App_Code folder, again like the question above, but the solution to that question (using the traditional HttpContext.Current.Session property) does not work either and yields the same error.

The strangest part to this is that it was working, but suddenly stopped. It was even deployed to production. It only stopped working once I started working on a different part of the pipeline. We were previously using a Web Service to expose server-side methods, and are now wanting to change to an custom ApiController. I've made the changes on my local machine, where the error is occurring. I can't imagine that being the reason for this new error due to the explanation below. I'm also using the same Session logic in a web part on the same page but for a different purpose, and that is functioning.

My debugging process to confirm that it's the SessionHelper method that's failing is as follows:

ObjectName obj = new ObjectName { property1 = "string" }
EventLogProvider.LogInformation("Class", "Method", obj.property1);

SessionHelper.SetValue("key", obj);

var obj1 = (ObjectName)SessionHelper.GetValue("key");
EventLogProvider.LogInformation("Class", "Method", obj1.property1);

The data is present and correct within the first Event Log, but a null reference error is thrown when trying to access obj1. I even tried simplifying this process by simply throwing a random string into the SetValue method like so:

SessionHelper.SetValue("key", "random text");
string str = ValidationHelper.GetString(SessionHelper.GetValue("key"), string.Empty);
EventLogProvider.LogInformation("Class", "Method", str);

Doing this returned a blank string in the Event Log.

Any advice would be appreciated.

Recent Answers


Brenden Kehren answered on August 18, 2023 18:46 (last edited on August 18, 2023 18:47)

You may want to share where you're trying to use this code. Is it in a custom module? Web part? Page Template? User control? And what development method are you using? Portal Engine? MVC? ASPX+Templates?

0 votesVote for this answer Mark as a Correct answer

Max De Los Reyes answered on August 18, 2023 18:55 (last edited on August 18, 2023 18:55)

Hi Brenden, thanks for the quick response.

It's none of the above, really. It's just custom code that we're housing in CMS/App_Code. We have custom providers, helpers, API controllers, and other code in there. I have web parts utilizing code from the class, but it's not a web part or module or anything of the sort itself.

We are also operating with the Portal Engine model.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on August 18, 2023 19:01

They should work just fine in your webparts, however, if you are using them in your api controllers you may get unexpected results. If you don't have the context of the site available, then you cannot use those Kentico methods without some odd things happening. If it worked prior, it was most likely out of luck.

My suggestion for you would be to NOT use session state and use something else, more robust.

0 votesVote for this answer Mark as a Correct answer

Max De Los Reyes answered on August 18, 2023 19:11

Not using SessionHelper methods directly in the API controller itself, but I do have an API controller utilizing the class that has the code above.

What would you suggest otherwise? I was using the SetValue method to hold onto data from an editable diagram (which really just amounts to a very dynamic object of arrays and objects within it, not my design and out of my hands). Users are able to save these to their profile, but if they're not logged in, we bounce them to the login page, then return them to the diagram page and load in the edited diagram from the Session.

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on September 1, 2023 18:14

Hi Max,

Please refer to this stackoverflow question, I think this is exactly your case: when you have WebApi controllers mixed into WebForms project session is not available by default.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.