Josh Smith

Follow me down the rabbit hole of cutting edge technology.
The Debugger Visualizer Item Template in VS2008 is Broken

Someone at Microsoft forgot to update the Debugger Visualizer item template for Visual Studio 2008.  Suppose that you add a visualizer to your project by selecting the "Debugger Visualizer" item template, as seen below:

The template automatically adds a bunch of boilerplate code so that you can easily get started with writing your own visualizer.  That's all well and good, but when you actually give your shiny new visualizer a test drive, you will be told that the CLR was "Unable to cast object of type 'Your Visualizer Type' to type 'Microsoft.VisualStudio.DebuggerVisualizers.DialogDebuggerVisualizer'." 

You can avoid this problem in several ways.  Naturally, you can take the high road and avoid using the item template in the first place.  If you are like me, that is a bad option because, in general, automation is a good thing.  Work smart, not hard!

You can do the "right" thing and actually fix the Debugger Visualizer item template itself.  That would probably be an interesting adventure into the world of Visual Studio template files, and would forever solve this problem on your machine.  But, if you are like me, this option sounds like too much work considering that you might never use the Debugger Visualizer item template again.  It isn't broken if you don't use it, right? ;)

It turns out that we can spend just a few seconds to fix the problem at hand.  Here's the problem: the Microsoft.VisualStudio.DebuggerVisualizers.dll assembly reference that is added to your project by the template is the wrong version.  Observe:

Notice how the Version of the assembly is 8.0.0.0.  That's the problem.  That's the version of the assembly which shipped with VS2005.  The Debugger Visualizer item template should have added Version 9.0.0.0 instead. All that you have to do is delete that assembly reference and add a new one, which references the correct version of the Microsoft.VisualStudio.DebuggerVisualizers.dll assembly.  The screenshot below shows the proper assembly to reference:

After you add that assembly reference, rebuild the project, and redeploy your visualizer, the exception seen previously does not get thrown.  Note, however, that if you intend on using your visualizer in both VS2005 and VS2008, you must have two separate builds since each version of Visual Studio requires a different version of that DebuggerVisualizers assembly to be referenced.

Posted: 15 Dec 2007, 18:24
Filed under: ,

Comments

Karl Shifflett said:

Josh,

Very nice find and thank you for sharing with the rest of us mortals.

Karl

# December 15, 2007 8:25 PM

Joshua Smith said:

Thanks Karl.  I hope this post saves people a lot of time when they are building their first visualizer in VS2008.  

Josh

# December 15, 2007 10:46 PM

Alex Sarafian said:

Presently I'm not in front of a Visual Studio 2008.But if backwords campatibility is maintained i think it shoud be fairly easy to correct the problem once and for all.

For Visual Studio 2005 the item template is this file

c:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033\Visualizer.zip for C#

Inside this zip file, Visualizer.vstemplate contains the reference that is added when using this visualizer. I think it should be fairly simple and worth the try instead of every time removing and adding the reference.

I'll get back to you when I get in front of VS2008 if you haven't already utilized the above.

# December 17, 2007 4:30 AM

Joshua Smith said:

Thanks Alex.  

# December 17, 2007 9:28 AM

Alex Sarafian said:

OK got it.

I went home first

in the C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\General\1033\Visualizer.zip\Visualizer.vstemplate you must replace the referenced assembly line with the according one.

Please replace this element

<Reference>

<Assembly>Microsoft.VisualStudio.DebuggerVisualizers</Assembly>

</Reference>

with this

<Reference>    <Assembly>Microsoft.VisualStudio.DebuggerVisualizers, Version=9.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A</Assembly>

</Reference>

# December 17, 2007 4:38 PM

Joshua Smith said:

Nice find, Alex.  Thanks for sharing it!

# December 17, 2007 4:45 PM

Alex Sarafian said:

You are welcome

# December 17, 2007 5:49 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS