03 April 2014

Removal of Windows Services Fails? Use the Correct Version of InstallUtil.exe

This is a narrative, following my resolution to removing a stubborn service that simply wouldn't let go.

Today, I tried using the Developer Command Prompt for VS2013 running as administrator, to remove a Windows Service I was developing in Visual Studio 2013 and testing on my workstation. Instead of removing the service, all I got was this lousy T-shirt. And this exception:
Exception occurred while initializing the installation:
System.BadImageFormatException: Could not load file or assembly 'file:///C:\Dev\ ... \MyService.exe' or one of its dependencies. An attempt was made to load a program with an incorrect format.
Huh. That's weird. The project was started in Visual Studio 2012 and not 2013. So, I tried using the Developer Command Prompt for VS2012 running ad Administrator; sometimes that will fix the issue. I launched it, and typed (well, copied and pasted):
cd "C:\Dev\ ... \Release"
installutil /u MyService.exe
Nope! I got the command displays an error identical to the first. I tossed this second T-shirt on my pile of conference shirts, and stepped back from the problem. So, if it isn't the .NET Framework version, and the path is definitely correct, something else must be different. I looked up the InstallUtil.exe in the usual place, in Windows Explorer:
C:\Windows\Microsoft.NET\Framework\v4.0.30319
But, then something caught my eye. The "Framework64" directory. Ah. Because my Windows Service is to be executed on a server, I targeted the x64 CPU instruction set in the build profile. In the developer command prompt window, I typed:
cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319installutil /u "C:\Dev\ ... \MyService.exe"
The utility happily complied, and I am free of the service.

Moral of the story:
You must use the proper x86 or x64 utilities, to match the target platform of the binary files.