System.BadImageFormatException:Could not load file or assembly … incorrect format when trying to install service with installutil.exe

27,130

Solution 1

There are two ways to solve this error

  1. Set Platform target to x86 for your project in visual studio(Right click on your solution and then go to properties then you will find build option and when you click on build you will find platform target). After you set your platform target to x86 try to do your unit test using nunit.

or

  1. Simply find out nunit-x86.exe in bin folder of your nunit folder and just run this exe file and you will not see any exception again :)

Solution 2

BadFormatException occurs when you attempt to load an x86 assembly in a 64bit process or vice versa. As per NUnit documentation (http://www.nunit.org/index.php?p=nunit-gui&r=2.4.2) the default runner is compiled as AnyCPU which means its a 64bit process on a 64bit machine. If one of your assemblies or any of its dependencies are directly compiled for x86 you will get this exception.

Switch to the Nunit-x86 or Nunit-console-x86.exe and everything should load correctly.

Solution 3

To tack onto Ammar's comment. Don't rely solely on what Configuration Manager tells you is the active platform. Go to the project properties and check there too, that is what is the real platform for the project

enter image description here

Share:
27,130
aami
Author by

aami

Updated on July 21, 2022

Comments

  • aami
    aami almost 2 years

    I know i am going to ask duplicate question but my scenario is totally different i think so because when i go to do unit testing with nunit tool of my program then in NUnit this error happen

    "NewTest.testtest (TestFixtureSetUp): SetUp : System.BadImageFormatException : Could not load file or assembly 'AUTO_REPAIR, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format."

    I wonder why this error is happening to this tool ? i am sure i dont have error in project or in any test case.

    Please help me out.

    here is image of this error

    enter image description here

  • C Bauer
    C Bauer over 10 years
    For anyone else that stumbled across this - if you're on x64 then switch all your projects over to that exclusively otherwise you'll continue having problems
  • KangarooWest
    KangarooWest over 8 years
    This led me to the right direction. My problem was almost the same, but it's actually due to the "Prefer 32-bit" box below Platform Target having been checked by default. I removed that check and it worked!
  • Guy Park
    Guy Park over 8 years
    If you have all your referenced DLL's set to AnyCPU, you can simply set the main executable (.exe) project to be the respective arch. I find this approach much easier to handle, since all you ever need to change is the main entry point (the .exe)
  • FrenkyB
    FrenkyB almost 7 years
    I've had 'AnyCPU' and x64 - on a x64 system. I've changed both to 'Any CPU' and error went away. Thanks for help.