ServerVariables empty unless Debug

Just had a really strange issue where I'm trying to log some ServerVariables when an error occurs.

The bug was when I ran the code without debugging, no ServerVariables were being logged.
If I went into Debug, and hovered my mouse over the ServerVariables variable, it had all the information I was looking for.
After that, it logged everything fine.

It seems called a Get on the ServerVariables variable was causing it to initialize properly.

After some further investigation, I found out I was relying on the NameValueCollection.HasKeys() method to see if I had any value.
This returns false. Just by calling Request.ServerVariables.Count, this completely fixes the problem.

So I changed the code as follows, and everything worked as expected

Changed From:

NameValueCollection nvc = Request.ServerVariables;

if(nvc == null) return;
if (nvc.HasKeys())
{
  //Get ServerVariables Information
}

Changed To:

NameValueCollection nvc = Request.ServerVariables;

if(nvc == null || nvc.Count == 0) return;
if (nvc.HasKeys())
{
  //Get ServerVariables Informatione
}

posted @ Wednesday, September 03, 2008 7:46 AM

Print

Comments on this entry:

# you can buy wow gold here!

Left by wow gold at 11/14/2008 2:28 AM
Gravatar
you can buy wow gold here!

# re: ServerVariables empty unless Debug

Left by roulette at 6/1/2009 9:44 AM
Gravatar
Fantastic post. Bookmarked this site and emailed it to a few friends, your post was that great, keep it up.

# re: ServerVariables empty unless Debug

Left by software development company at 8/28/2009 6:23 PM
Gravatar
Hey, that was interesting,

Thanks for bringing this up

Your comment:



 (will not be displayed)


 
 
 
Please add 6 and 8 and type the answer here:
 

Live Comment Preview:

 
«March»
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910