Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.BadImageFormatException when installing service

I am getting below exception when I try to run installer to set up service:

# ./InstallUtil ../../../../Applications/GasPosApp/bin/Wrapper.exe -s ../../../
../Applications/GasPosApp/properties/DispenserViewerApplication.wrapper.conf
Microsoft (R) .NET Framework Installation utility Version 4.0.30319.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Exception occurred while initializing the installation:

System.BadImageFormatException: Could not load file or assembly 'file:///C:\Appl
ications\GasPosApp\bin\Wrapper.exe' or one of its dependencies. The module was e
xpected to contain an assembly manifest..
like image 220
user1280096 Avatar asked Oct 23 '13 12:10

user1280096


3 Answers

You have to use the right version of the InstallUtil. If you are trying install a 32 bit service (x86), use

c:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe

and if you are installing a 64 bit service (x64), use

c:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe

Note, that the last specified folder (v4.0.30319) may differ a machine from machine. If you have only an older version of the framework available you can encounter one of the following folders instead - v1.0.xxxx, v1.1.xxxx, v2.0.xxxxx, v3.0, v3.5.

like image 74
Ondrej Janacek Avatar answered Nov 14 '22 13:11

Ondrej Janacek


This is often an indication of a mismatch between 32 and 64 bit. For instance if Wrapper.exe is compiled as x86 and you install it using the 64 bit InstallUtil the process cannot load the 32 bit Wrapper.exe assembly and will throw a BadImageFormatException.

The solution is to use the 32 bit InstallUtil placed in the Framework (not Framework64) subfolder of the .NET folder (e.g. C:\Windows\Microsoft.NET\v4.0.30319\Framework if you are using .NET 4 or 4.5).

like image 3
Martin Liversage Avatar answered Nov 14 '22 12:11

Martin Liversage


I agree with all here! This error come, if you are trying to load your application or a one of the refreanced assemblies in an invalid operating system format.

http://msdn.microsoft.com/en-us/library/system.badimageformatexception(v=vs.80).aspx

  • Check your opearting system format? x86 or x64
  • Check your project build configuration?
  • Check all refreanced assebmlies (VERY IMPORTENET) if one of your loaded refreances (DLL's) is built in the wrong format you will get this error also (Correct me if I'm wrong!).
  • If possible change your VS buid configurartion to AnyCpu.

Microsoft has created useful tool called CorFlags which can be used to show or set the targeted platform of an managed assembly.

C:\Program Files\Microsoft.NET\SDK\v2.0\Bin\CorFlags.exe

Syntax:
CorFlags filename

**anycpu** 
Version   : v4.0.30319
CLR Header: 2.5
PE        : PE32
CorFlags  : 1
ILONLY    : 1
32BIT     : 0
Signed    : 0 

**x86**
Version   : v4.0.30319
CLR Header: 2.5
PE        : PE32
CorFlags  : 3
ILONLY    : 1
32BIT     : 1
Signed    : 0 

**x64** 
Version   : v4.0.30319
CLR Header: 2.5
PE        : PE32+
CorFlags  : 1
ILONLY    : 1
32BIT     : 0
Signed    : 0 
like image 3
Bassam Alugili Avatar answered Nov 14 '22 14:11

Bassam Alugili