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:
"ExistingKey"
key exists and has a value of false (if key
doesn't exists returns false).!=null
it returns false (first thing weird).?:
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.
There is no difference. The getter for Page. Session returns the context 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.
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.
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:
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With