Fresh Click Media

Web and Software Development

September 30, 2011
by admin
0 comments

If a unit test connects to a database, interacts with the file system, reads a configuration file value, requests some data from a web service, it isn’t a unit test.

December 8, 2010
by shane
0 comments

How to create a Custom Debug Visualizer in Visual Studio

In this screencast, I talk through the process of creating a simple custom debug visualizer to view a System.Drawing.Image image.

Essentially, the visualizer is a class library that references the Microsoft.VisualStudio.DebuggerVisualizers and System.Windows.Forms assemblies. The project has a class, ImageVisualizer, that inherits the .NET framework DialogDebuggerVisualizer class, and overrides its Show method:

class ImageVisualizer : DialogDebuggerVisualizer
{
    protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
    {
        Image image = (Image)objectProvider.GetObject();
        ImageForm form = new ImageForm { DebugImage = image };
        windowService.ShowDialog(form);
    }
}

This converts the variable that’s being debugged to an Image type using the objectProvider parameter’s GetObject method call. This is then assigned to the dialog’s DebugImage property, and the dialog is shown using the windowService parameter’s ShowDialog method.

The actual dialog is very simple indeed, comprising of just a PictureBox to show the image, and a close button to, well, close the dialog.

public Image DebugImage
{
    set
    {
        this.pictureBoxImage.Image = value;
        this.pictureBoxImage.SizeMode = PictureBoxSizeMode.Zoom;
    }
    get
    {
        return this.pictureBoxImage.Image;
    }
}

The dialog class’ DebugImage property simply assigns the picture box’s Image property and zooms. I kept things simple for the screencast, but there’s no reason why the dialog couldn’t be extended to show other image properties such as image location, image type, and dimensions.

To use the visualizer, simply build the project and copy the dll to the Visualizers directory, which on my machine is:

C:Program Files (x86)Microsoft Visual Studio 10.0Common7PackagesDebuggerVisualizers

This particular path relates specifically to my machine, since I’m running 64 bit windows, and Visual Studio 10. Once the dll has been copied, it can be used on the next debug session. In the screencast, I use a simple console program example that loads an image from file. Clicking on the magnifying glass opens up the visualizer’s dialog, and the image can be seen.


Continue reading

November 27, 2008
by shane
7 Comments

Programmatic impersonation in C#

Impersonation

I recently deployed a WPF app on a server that allowed the user to stop and start some application-related services. The purpose of the app was to allow users with administrative rights an easy way to manage the services that they needed to manage. Granted, they could manage the services through the services MMC, but the little WPF app was a requirement, and it’s our job as developers to make things easier for our clients – right?

All went well until a change of requirements meant that a user without administrative rights needed to use the program to stop and start the required services. When I tried to use the app, I got an exception – quite rightly, stopping and starting the services required admin rights. We needed the restricted user to be able to log on and use the app, but still needed to restrict their permissions.

So – step in programmatic impersonation in C# – a way to give restricted users the power that that’s required, all within the confines of your application. Continue reading