17 October 2012

How to generate missing Fakes shims

While using the Fakes (Moles) isolation framework, you will notice some mscorlib shims are missing, particularly in the System namespace. For example, System.Fakes.ShimEnvironment does not exist. Although these are missing by default, you can request the compiler to generate shims for specific objects. This post provides steps to request Fakes shim generation for System.Environment and an example of implementation.

This tutorial targets Microsoft Fakes Isolation Framework on Visual Studio 2012 Ultimate or later; but, this process is also compatible with the developmental version, project name "Moles", running on Visual Studio 2008 or 2010. This tutorial assumes you have basic understanding of the MSTest framework.

20 September 2012

Fakes added to Moles posts

Today, I am (finally) starting to update all of my posts on use of the Moles Isolation Framework to its official release version, called "Fakes Framework", included in Visual Studio 2012 Ultimate and newer.

To learn how to use both Moles and Fakes, click the "Unit Testing with Fakes/Moles Framework" link, below the page title on this blog.  Please bear with me, while I get the information updated.  I am very glad to see Moles come to market!

16 August 2012

What To Do When Windows Service Will Not Delete


When a service is removed from any Microsoft Windows operating system, to date, the system must be rebooted, to completely remove the service.  After restarting the operating system, the service entry should be deleted.  If the service still appears in the Services MMC, or a "specified service already exists" error is displayed upon installing a fresh copy of the service, you'll probably need to tweak the registry, to manually remove the service key.

BE VERY CAREFUL, WHEN EDITING THE SYSTEM REGISTRY!

  1. Execute regedit.exe
    1. Windows Vista/2008 and newer: type "regedit" in the start menu search box
    2. Older versions: click the "Run" option, located in the start menu
  2. Press the Enter key, to launch
  3. Back up your registry
    1. Click file menus: File > Export...
    2. Select a location to save the registry backup file
    3. Click the Save button
  4. Using the tree menu, navigate to: HKEY_LOCAL_MACHINE > SYSTEM> CurrentControlSet > services
  5. Locate the child node of "services" that matches the service you need to delete
  6. Press the delete key on the keyboard -- a delete confirmation appears
  7. Click the Yes button, in the delete confirmation -- the registry key is deleted
  8. Close the registry editor

How To Work Around "Specified Service Already Exists"

If you have ever created a Windows Service, you are likely familiar with the "specified service already exists" installation error message.  Most developers encounter this situation after making code changes, and attempting to install the updated service, for testing.  The problem is that Windows services can not be removed without a system reboot.

The solution is simple.  Install the service for testing, only once.  To test newly-compiled changes to the service:

  1. Stop the service
  2. Copy new files into the service installation directory, overwriting the old ones
  3. Start the service

Easy!

As always, use unit tests, to test code, instead of testing through execution.  There is little excuse to not test code, these days.  To expedite testing and TDD, I suggest installing NCrunch for Visual Studio (http://www.ncrunch.net/).

27 March 2012

Creating a Comprehensive Event Logger

Would you rather try to decipher the meaning of an error in the Windows application event log, or ram a fork into the back of your hand?  I thought so.

The problem with the event logs is they are notoriously uninformative.  It seems that their purpose is to taunt IT personnel, by suggesting some helpful information is available. Yeah, right.  There are entire Web sites dedicated to deciphering event identification numbers mean.  The necessity for such sites is just plain sad.  Apparently, leaving what happened a mystery is a better alternative to including actual WORDS that describe the problem, in human language.

I implore you: do not be that kind of developer!  I always insist on making everything human readable, and easy to understand.  By leveraging the extensible nature of .NET, we can easily log useful and meaningful information to the event log, which will help the user or IT administrator fix the problem, and/or help the developer debug the issue.

Here's how you can use a single method, to alleviate the IT admin's urge to strangle you.

28 February 2012

Suppress EF Compiler Warnings Error 11007 and 11008

I am working on an MVVM project that parses records out of encrypted data files, on demand.  There is no real data source for this project, for Entity Framework 4.1 to reference.  Every time I build the model assembly, I get a huge pile of warnings for Error 11007 and 11008.

Because I am a savvy developer (and slightly obsessive-compulsive), I don't want to see any compiler warnings after a build.  Because I know these are non-issue warnings, I can safely disable them.  Here's how to disable EF warnings in your projects: