Monday 8 June 2015

Could not load file or assembly '' or one of its dependencies. An attempt was made to load the program with an incorrect format.

 
 

 

Could not load file or assembly '' or one of its dependencies. An attempt was made to load the program with an incorrect format.

If you have any VC++ CLI components in your azure web role and you are facing the above error, then this post might be helpful for you to resolve your issue.
 

One of the most common problems reported regarding Windows Azure Web Sites is “Assembly Loading” issue.

Two main things you need to verify,
  • This error might be causing because Azure runtime is unable to find the dll/assembly that it needs to run the web role.
  • Secondly, this error might occur if you are trying to run any of the 32-bit components in windows azure.
 When you create a web role and try to host on Windows Azure you may seen this error because the Azure runtime was unable to load the necessary assemblies to make your web role run successfully.
 
Now, the question is how to identify which assembly is not loading as expected:

First check:
There is a program called Dependency Walker which allows you to see the dependencies of a given PE file (dll, exe, ocx...).
This error is really annoying, and very difficult to debug. You have to make sure that your dll is present as well as ANY dependency this dll has.
If you see that any dll/assembly is missing ensure you package that dll into your azure package.

Second Check:
Make sure you run your web role and all the other project (mainly VC++ CLI Components) inside the webrole in "Release" Mode. The reason, few VC++ debug dll cannot be available in Azure Emulator and might cause this issue. :) (This was my scenario, I struggled one week to identify this).
 


 Third Check:
Few additional settings you need to verify, Check whether you AppPool is enabled for 32-bit applications.
 
Go to Respective ==> Site Application Pool ,
Click on ==> Advance Settings
Change value of ==> "Enable 32-Bit Applications" to True.

 Fourth Check:
 
The app would run fine on the developer machines and select others machines but not on recently installed machines. In this case, you need to verify whether the respective Visual C++  runtime installed or not. Adding the respective Visual C++ Runtime redistributable to the app installer will fix the issue.
 
And finally,
Ensure you have packages all the respective dll into your Azure packaging, to do that
1. If you want an your file to be copied, first you can add it to the project.
2. After adding right click on the file --> properties
3. Set the Build Action from "None" to "Content"
4. or The other option you have is to keep the Build Action as "None" but set "Copy to Output Directory" to "Copy if Newer" (or "Copy Always"). 
 This will include your file in Azure packaging.

Hope this resolves your issue, Happy coding..

 
1