Visual Studio logo

Fixing the “Could not load file or assembly … or one of its dependencies” error

This post was most recently updated on May 26th, 2019.

Reading Time: 2 minutes.

This post describes how to fix the “Could not load file or assembly ‘[assemblyname]’ or one of its dependencies. An attempt was made to load a program with an incorrect format.” error. 

The solution described in this post shows you how to resolve the issue when you’re deploying from Visual Studio, but get the error “Could not load file or assembly or one of its dependencies” when deploying to IIS or when opening your website after the deployment.

Problem

Especially while installing a new dev machine, and building your project for the first time, you may end up getting the following exception:

Could not load file or assembly ‘[assemblyname]’ or one of its dependencies. An attempt was made to load a program with an incorrect format.

No fear, though, as this is usually easily fixed. In quite a few cases, it’s simply a mismatch between architectures and easily changed.

Reason

The latter part of the error message should point you to the right direction. “An attempt was made to load a program with an incorrect format.” That means that the assembly, which was to be loaded, was in an unexpected format. The format, in this case, refers most likely to 64-bit build of an application being deployed to IIS, which is being run in 32-bits.

By default, Visual Studio uses 32-bit version of IIS Express for your deployments. In case you know what you’re doing, it’s safe and easy to change this, though. If you’re building your solution in x64, running it in 32-bit IIS Express won’t work.

In my case, I was building a 64-bit application. Therefore I also had to change the IIS to run in 64-bit version. That’s luckily quite easy!

Solution: Change the IIS Express to run in 64b

Okay – see the steps below.

How to change IIS Express to run in 64-bit

  1. You’ll need to enable 64-bit IIS Express from VS settings:
  2. Navigate to Visual Studio – Tools – Options – Projects and Solutions – Web Projects
  3. Select “Use the 64 bit version of IIS Express for web sites and projects
  4. Hit “OK” to save your changes.

See the screenshot below:

IIS Express 64-bit version - how to fix the "Could not load file or assembly..." error
IIS Express 64-bit version – how to fix the “Could not load file or assembly…” -error

And you should be golden!

Let me know whether it worked or not.

Antti K. Koskela

Antti Koskela is a proud digital native nomadic millennial full stack developer (is that enough funny buzzwords? That's definitely enough funny buzzwords!), who works as a Solutions Architect for Valo Intranet, the product that will make you fall in love with your intranet. Working with the global partner network, he's responsible for the success of Valo deployments happening all around the world.

He's been a developer from 2004 (starting with PHP and Java), and he's been bending and twisting SharePoint into different shapes since MOSS. Nowadays he's not only working on SharePoint, but also on .NET projects, Azure, Office 365 and a lot of other stuff.

This is his personal professional (e.g. professional, but definitely personal) blog.
mm

2
Leave a Reply

avatar
5000
1 Comment threads
1 Thread replies
2 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
Antti K. KoskelaKiet Chi Luu Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Kiet Chi Luu
Guest
Kiet Chi Luu

Hi there,

I do have a quick question. I have a 32-bit web app. I had to incorporate a 64-bit Oracle.DataAccess.dll. I checked the “Use the 64 bit IIS Express for websites and projects” in order to get it to build and run on my local machine. However when I deployed it to my test webserver, I got the incorrect format error. From what I read, I understand that an application can not run both 32 bit and 64 bit dlls in the same project. My application does use both bit types. I was wondering how the application is able to run on my local machine, but not when it is deployed. My test webserver uses IIS7.

Thank you for any replies,
Kiet