I have a Windows service written in C# using Visual Studio 2010 and targeting the full .NET Framework 4. When I run from a Debug build the service runs as expected. However, when I run it from a Release build I get a System.BadImageFormatException (details below). I've been searching the internet for a solution but so far every thing I've found hasn't helped me find a solution.
The problem exists on both Windows 7 64-bit (dev) and Windows XP SP3 32-bit (target) systems.
Here is what I've tried so far:
System.BadImageFormatException was unhandled
Message=Could not load file or assembly 'XxxDevices, Version=184.108.40.206, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
FileName=XxxDevices, Version=220.127.116.11, Culture=neutral, PublicKeyToken=null
FusionLog=Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable c:\Dev\TeamE\bin\Release\XxxDevicesService.vshost.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: User = XXX
LOG: DisplayName = XxxDevices, Version=18.104.22.168, Culture=neutral, PublicKeyToken=null
LOG: Appbase = file:///c:/Dev/TeamE/bin/Release/
LOG: Initial PrivatePath = NULL
Calling assembly : XxxDevicesService, Version=22.214.171.124, Culture=neutral, PublicKeyToken=null.
LOG: This bind starts in default load context.
LOG: Using application configuration file: c:\TeamE\bin\Release\XxxDevicesService.vshost.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///c:/TeamE/bin/Release/XxxDevices.DLL.
ERR: Failed to complete setup of assembly (hr = 0x8007000b). Probing terminated.
at XxxDevicesService.Program.Main(String args)
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String args)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
Verified build settings such as Platform Target are all the same (x86).
That's not what the crash log says:
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64
Note the 64 in the name, that's the home of the 64-bit version of the framework. Set the Target platform setting on your EXE project, not your class library project. The XxxDevicesService EXE project determines the bitness of the process.