Oracle 64-bit assembly throws BadImageFormatException when running unit tests

We recently upgraded to the 64-bit Oracle client. Since then, Visual Studio 2010 unit tests that hit the database (I know, unit tests shouldn't hit the database--they're not perfect) all fail with this error message:

Test method MyProject.Test.SomeTest threw exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.BadImageFormatException: Could not load file or assembly 'Oracle.DataAccess, Version=, Culture=neutral, PublicKeyToken=89b483f429c47342' or one of its dependencies. An attempt was made to load a program with an incorrect format.

I resolved this by changing the test settings to run tests in 64-bit. From the Test menu, go to Edit Test Settings, and pick your settings file. Go to Hosts, and change the "Run tests in 32 bit or 64 bit process" dropdown to "Run tests in 64 bit process on 64 bit machine". Now your tests should run.

This fix makes me a little nervous. Visual Studio 2010 and earlier seem to change that file for no apparent reason, add more settings files, etc. If you're not paying attention, you could have TestSettings1.testsettings through TestSettings99.testsettings sitting there and never notice the difference. So it's worth making a note of how to change it in case you have to redo it, and being vigilant about files VS tries to add.

I'm not entirely clear on why this was even a problem. Isn't that the point of an MSIL assembly, that it's not specific to the hardware it runs on? An IL disassembler can open the Oracle.DataAccess.dll in question, and in its Runtime property, I see the value "v4.0.30319 / x64". So I guess the assembly was specifically build to target 64-bit platforms only, possibly due to a 64-bit-specific difference in the external Oracle client upon which it depends. Most other assemblies, especially in the .NET Framework, list "msil", and a couple list "x86". So I guess this is another entry in the long list of ways Oracle refuses to play nice with Windows and .NET.

If this doesn't solve your problem, you can read others' research into this error, and where to change the same test setting in Visual Studio 2012.


  • Hi there,

    I don't know if this is relevant, as I don't run unit tests against the database, however...

    At work I develop on a 32 bit Win XP (urrrgh!) client. But I write a lot of Windows services that run on 64 bit Windows 2008 R2 Server.

    What I have found is that to make everything "play nicely", every DLL, executable etc that you compile MUST target Any CPU in the build properties. If you don't it won't work!

    Hope this helps!



  • If you see oracle dataccess assembly in reflector, you can see it makes a call to com which it why it is built for x634 and x32 specifically

Comments have been disabled for this content.