Friday, May 24, 2013

installutil.exe - System.BadImageFormatException Could not load file or assembly or one of its dependencies

A .NET framework utility, installutil.exe, is used to install or uninstall .NET assembly to Windows Service. To uninstall existing service and reinstall new binaries, one can create a batch as below. That is, stop existing service and uninstall existing service. And then install new service and start it again.
net stop "My Service"
%SystemRoot%\Microsoft.Net\Framework\v4.0.30319\installutil.exe /u MyService.exe
%SystemRoot%\Microsoft.Net\Framework\v4.0.30319\installutil.exe MyService.exe
net start "My Service"

The script worked fine but one day the batch was failed with the following error.

Exception occurred while initializing the installation:
System.BadImageFormatException: Could not load file or assembly 'file:///D:\Services\MyService.exe' or one of its dependencies. An attempt was made to load a program with an incorrect format..
As it turned out, the MyService.exe assembly happened to be 64bit binary and the installutil.exe was for 32 bit. So changing the installutil.exe path to 64 bit folder made it working.
%SystemRoot%\Microsoft.Net\Framework64\v4.0.30319\installutil.exe MyService.exe