Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpContext.Current.Session unclear behaviour boolean

I'm having a weird behaviour trying to get the value of a boolean property stored at HttpContext.Current.Session.

The object is a boolean. First I'm trying to check if the object exists and, if exists, use it.

I'm trying to use it in a ?: operator but it behaves strange. Here is my Watch window: Watchers from VisualStudio

Premise

  • The "ExistingKey" key exists and has a value of false (if key doesn't exists returns false).

Results

  1. When checking if !=null it returns false (first thing weird).
  2. When using the ?: operator, besides the condition is false, it returns the first expression, 4 (second thing weird).

Could somebody explain this behaviour?


Note: I'm not asking for an alternative to bypass this situation. Just asking why this works like this.

like image 416
Mario Levrero Avatar asked Aug 26 '16 08:08

Mario Levrero


People also ask

What is difference between session [] and HttpContext current session []?

There is no difference. The getter for Page. Session returns the context session.

What is use of HttpContext current session?

HttpContext. Current. Session simply returns null if there is no session available. The HttpApplication's implementation of the Session property throws an HttpException with the message Session state is not available in this context.

How do I get HttpContext session?

Unlike previous ASP.Net applications, the Session object is not available under the HttpContext. Current property, instead it is available in the HttpContext class in ASP.Net Core applications. Session can be enabled using the Configure method.


2 Answers

It seems to be some sort of bug within the watch window. I tested the following code:

protected void Page_Load(object sender, EventArgs e)
{
   var  objDict = new Dictionary<string, object>();
    var boolDict = new Dictionary<string, bool>();
    Session["ExistingValue"] = false;
    bool? nullableValue = false;

    Session["ExistingValueNullable"] = nullableValue;
    var existingValue = (bool)Session["ExistingValue"];
    var existingValueIsNotNull = Session["existingValue"] != null;
    objDict["ExistingValue"] = false;
    boolDict["ExistingValue"] = false;
    bool existingValueIsNotNullIf = false;
    if (Session["ExistingValue"] != null)
    {
        existingValueIsNotNullIf = true;
    }
}

And I got the following in the watch window:

Watch Window

So you can see that in the case of Session and a Dictionary<string,object> the != null evaluates to false. A Dictionary<string,bool> evaluates this comparison properly.

Weirdly, 'Session["ExistingValue"] != null' and 'Session["ExistingValue"]' == null' are both false.

If I cast the session value to a bool first then compare to null, I get the correct result.

Finally, if I test the value 'Session["ExistingValue"] != null' in code, I get a proper result. Which at least reassures me that this is something in the watch window, and there shouldn't be a similar issue in code.

All my testing was in Visual Studio 2015.

like image 118
MikeS Avatar answered Oct 06 '22 00:10

MikeS


Please do not consider this an answer at the moment, the following is much easier to write in an answer than in a comment due to space and formatting constraints.

I agree with the comments on the question, line 3 is not consistent with the results of the other lines. The only thing I can think of that could explain this is that the Watch window in Visual Studio has stale data / has a corrupted state. I think executing the same statements but in the code itself could prove or refute this. The following code is the same as what you have but output to a StringBuilder. Could you execute this and post the resulting string and let us know if this is any different from what you have in your Watch window?

var session = HttpContext.Current.Session;
var builder = new System.Text.StringBuilder();

builder.AppendFormat("session[\"someKeyThatDoesNotExist\"] => value {0}", session["someKeyThatDoesNotExist"] ?? "null").AppendLine();
builder.AppendFormat("session[\"ExistingKey\"] => value {0}", session["ExistingKey"] ?? "null").AppendLine();
builder.AppendFormat("session[\"ExistingKey\"] != null => value {0}", session["ExistingKey"] != null).AppendLine();
builder.AppendFormat("session[\"ExistingKey\"] != null ? 4 : 5 => value {0}", session["ExistingKey"] != null ? 4 : 5).AppendLine();
builder.AppendFormat("session[\"ExistingKey\"] == null ? 4 : 5 => value {0}", session["ExistingKey"] == null ? 4 : 5).AppendLine();

var totalDebugInfo = builder.ToString();
like image 38
Igor Avatar answered Oct 06 '22 00:10

Igor