I came across “System.BadImageFormatException” error while trying to migrate an application from Windows Server 2008 R2 to Windows Server 2012 R2. I will list possible root cause and solution for this issue in this post.
Error message:
Attempt to load Oracle client libraries threw BadImageFormatException. This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed.
Root cause
The most common cause of the BadImageFormatException is that an incompatible DLL file which is referenced in the application. The incompatibility is probably due to CPU architecture.
For example: The application targets 64-bit CPU but the DLL is 32-bit OR the application targets 32-bit CPU but the DLL is 64-bit.
If you want to find out the CPU architecture of a DLL file, check this post out: How to find out processor architecture (x86, x64) of dll and exe files?
Solution for System.BadImageFormatException
In my case, the issue was related to the Oracle DLL. The application targeted 64-bit CPU. Therefore, the issue (and the solution) could be one of these:
- In the Visual Studio, 32-bit Oracle DLL is referenced and the application is compiled with this DLL. If the application currently works, this is not the case. In order to double check,go to application folder and find the Oracle DLL (It should be in the bin folder). Check if it is 32-bit or 64-bit. If it is 32-bit, you can manually replace it with the 64-bit DLL or you can recompile the application by referencing 64-bit DLL
- The application is configured to use Oracle DLL that is installed in the server. The DLL installed is 32-bit. This scenario seems to be more accurate for this case. Even if you installed both 32-bit and 64-bit Oracle DLLs in the server, application might be still picking up the 32-bit in runtime. If this is the case, you can solve the issue by:
- Copying 64-bit DLL to the bin folder in the application directory
- Removing Oracle 32-bit DLL from the server if there is no other applications need it
Note: Even if you match DLL CPU architecture, you may still run into this issue if the application is not compatible with the targeted DLL. For example: The application may not be compatible with Oracle 12c even though you install 64-bit Oracle DLLs. You can try getting it work with a lower version such as Oracle 11g.
If you need help finding out the Oracle version you are using, please check this post: How to find out which Oracle Client versions are installed?
OK well I had this same problem. I did less than all this. I downloaded the more or less latest ODAC 32 bit client for Oracle (Oracle.DataAccess.dll) from Ora.122.19.1 – bur I don’t think the version is that critical.
I added a line to web.config right before the oracle.manageddataaccess.client entry:
I added the configuration section:
<!–
–><!–
–><!–
–>
<!– –>
<!—->
I explicitly added a reference to the dll in Visual Studio, and made sure the actual dll file was copied into the bin subdirectory.
I simplified my connectstring, which was choking my ORM, to just connect to the host and authenticate – no special cStr attributes.
That was all.
Well this thing ate the markup, but the configuration sections are per the Oracle documentation.