Friday 26 November 2010

Problem with System.Guid.NewGuid returning Empty after some time

http://www.thesoftwaredevotional.com/2008/12/guid-visualizer-broken-in-vbnet.html

GUID Visualizer Broken in VB.NET and How to Fix It


A colleague brought over a strange problem he was seeing while debugging a VB.NET project. During a debugging session he hovered his mouse over a variable of type System.Guid. You normally expect the standard visualizer for the type to appear, but it was displaying "Empty". Unfortunately, this led to some thrashing and gnashing of teeth because this 'bug' caused him to incorrectly suspect a problem with his code.

After a bit of searching and reflecting over the System.Guid class, I found that it is the visualizer for GUIDs in VB.NET that is 'broken'. The 'bug' exists in both Visual Studio 2005 and Visual Studio 2008. Dang.

Here is what the problem looks like out in the wild. In VB.NET, the visualizer tricks you into thinking that the value of the GUID is Empty.



When you call .ToString on the GUID, there is clearly a GUID populated.


I ran the *same* code in C#, and it works perfectly. +1 for C#!



Microsoft left a bug in the two most recent versions of Visual Studio? Not really.

When I found that the GUID shows up correctly when you call the .ToString method, I realized that the GUID is populated, it just doesn't appear correctly in the debugger. This led me to believe that there must be something else going on. I got out Reflector to take a look at the System.Guid class and found the problem.

As it turns out, the System.Guid class contains a single read-only property named Empty. Because the Empty property shows up in the debugger visualizer as the only member, it has the effect of making you think that the value is empty. To make matters worse, the Empty property returns an empty (all zeros) instance of a System.Guid class. The result, when you drill into the System.Guid variable: you get a never-ending tree of what appears to be Empty GUIDs.


We can't blame the visualizer specifically. The word Empty is misleading and leads developers to believe that a GUID variable is empty - the "not used" type of empty.

You can work around this problem by downloading a visualizer for GUIDs (from here and here). Both of these visualizers give you the option to open a window in the debugger displaying the GUID's value. A little cumbersome.

Here's how you can create an even better GUID debugger visualizer on your own.

Create a VB.NET class library. If you are using Visual Studio 2008, make sure to target the .NET 2.0 framework. Create a new class and put the code below into the class file. The class and project names do not matter.


Public Class Visualizer_GUID
'---- no implementation necessary
End Class

Compile the code and put the output in your "My Documents\Visual Studio 2008\Visualizers" and/or "My Documents\Visual Studio 2005\Visualizers" directories. Crank up another Visual Studio project, create a GUID, and hover over it in debug mode. Here's what you will see.


Now that's much better.