324

The exact error is as follows

Could not load file or assembly 'Microsoft.SqlServer.Replication, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. An attempt was made to load a program with an incorrect format.

I've recently started working on this project again after a two month move to another project. It worked perfectly before, and I've double checked all the references.

12 Answers 12

405

The answer by baldy below is correct, but you may also need to enable 32-bit applications in your AppPool.

Source: http://www.alexjamesbrown.com/uncategorized/could-not-load-file-or-assembly-chilkatdotnet2-or-one-of-its-dependencies-an-attempt-was-made-to-load-a-program-with-an-incorrect-format/

Whilst setting up an application to run on my local machine (running Vista 64bit) I encountered this error:

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

Obviously, the application uses ChilKat components, but it would seem that the version we are using, is only the 32bit version.

To resolve this error, I set my app pool in IIS to allow 32bit applications. Open up IIS Manager, right click on the app pool, and select Advanced Settings (See below)

enter image description here

Then set "Enable 32-bit Applications" to True.

enter image description here

All done!

10
  • Would this cause the entire application pool to run as 32-bit, and not full 64-bit? Would re-compiling the dependency for 64-bit / all platforms be a better solution? I'm having this issue due to NFOP, but since it's open source, I could re-compile it. Commented Jan 18, 2011 at 9:12
  • 2
    The link doesn't work, but your explanation was enough to solve my problem. +1 Commented Mar 22, 2011 at 22:53
  • Link with instructions on how to enable 32-bit applications: blogs.msdn.com/b/rakkimk/archive/2007/11/03/…
    – Brandon S
    Commented Mar 28, 2011 at 15:33
  • 15
    It might be worth noting that this AppPool solution only applies to ASP.NET applications - could confuse some devs that never used/configured ASP.NET or IIS.
    – galaktor
    Commented Apr 11, 2011 at 7:38
  • does not fix it in IIS7 only in IIS7.5 Commented Jan 12, 2013 at 0:23
120

I've found the solution. I've recently upgraded my machine to Windows 2008 Server 64-bit. The SqlServer.Replication namespace was written for 32-bit platforms. All I needed to do to get it running again was to set the Target Platform in the Project Build Properties to X86.

2
  • I'm having this problem with a DLL, but my Project > HDP Properties... does not have a "Build" tab (seemingly everything but, but not that). Both the Compile and Debug tabs have a "Platform" dropdown, but the only item in the dropdown in both cases is "Active (Any CPU)". Commented Aug 26, 2013 at 19:12
  • I happened to encounter the reverse of the above problem. I had everything running in 64 bit and my Build target was Any CPU. When I changed this to 64-bit it worked perfectly fine :) Commented Jul 11, 2017 at 17:44
91

Change the value for Platform Target on your web project's property page to Any CPU.

enter image description here

5
  • Has no effect, unfortunately.
    – Jonathan
    Commented Aug 6, 2011 at 3:43
  • 5
    I was wrong - this worked, after I got rid of all my 'bin' folders. Thanks!
    – Jonathan
    Commented Aug 6, 2011 at 4:19
  • Also fixed my problem, but only after doing this for EVERY project in my solution. The steps mentioned are for C# projects. For VB.NET projects, change Project Properties > Compile > Advanced Compile Options > Target CPU to "AnyCPU" Commented Sep 15, 2011 at 9:44
  • 6
    In my case, the project was already targeting "Any CPU". But I'm running 64-bit Windows and was trying to DllImport an old-school hardware driver. Specifically changing the target to x86 fixed it for me.
    – bsegraves
    Commented Nov 14, 2011 at 16:00
  • In my case, I wanted to run under x64. It wasn't working. After changing all recommendations, I also had to change the 'Bitness' under the Web also to x64
    – Nathan
    Commented Apr 2, 2018 at 4:59
53

Go to IIS -> Application Pool -> Advance Settings -> Enable 32-bit Applications

1
  • Clay, Which IIS version are you using ?
    – Harpal
    Commented Nov 27, 2013 at 11:19
26

We recently had the issue when trying to run the code from Visual Studio. In that case you need to do
TOOLS > OPTIONS > Projects and Solutions > WEB PROJECTS and check the "Use the 64 bit version of IIS Express for web sites and projects".

3
  • 1
    This is what ultimately worked for me. Especially for folks trying to debug Web API with Service Fabric libraries!!! If you are using any Service Fabric DLLs in your project, make sure you are using the above solution. Commented Sep 1, 2016 at 0:45
  • This is the ONLY solution that worked for me. Thanks. :D Commented Jan 30, 2017 at 16:32
  • Work for me too!
    – Harry
    Commented Mar 13, 2017 at 11:16
11

For those who get this error in an ASP.NET MVC 3 project, within Visual Studio itself:

In an ASP.NET MVC 3 app I'm working on, I tried adding a reference to Microsoft.SqlServer.BatchParser to a project to resolve a problem where it was missing on a deployment server. (Our app uses SMO; the correct fix was to install SQL Server Native Client and a couple other things on the deployment server.)

Even after I removed the reference to BatchParser, I kept getting the "An attempt was made..." error, referencing the BatchParser DLL, on every ASP.NET MVC 3 page I opened, and that error was followed by dozens of page parsing errors.

If this happens to you, do a file search and see if the DLL is still in one of your project's \bin folders. Even if you do a rebuild, Visual Studio doesn't necessarily clear out everything in all your \bin folders. When I deleted the DLL from the bin and built again, the error went away.

1
  • I just did a rebuild and the issue went away without having to manually deleting the DLL. Thanks!
    – Trevor
    Commented Dec 14, 2011 at 20:46
6

I had this in an MVC5 app in Windows 10 against IIS Express. My solution was the following:

  • Tools =>
    • Options =>
      • Projects and Solutions =>
        • Web Projects =>
          • Use the 64 bit version of IIS Express for web sites and projects
3

change it to 32-bit (true) it works

if you get this Length cannot be less than zero. Parameter name: length issue in iis server configuation do the simple thing change the connection string in web.config file like your sql server name and server name and restart iis then try to load the page it works

2

If Publishing in Visual Studio 2012 when erroring try unchecking the "Procompile during publishing" option in the Publish wizard.

Un-check "Precompile during publishing"

0

in windows form application I do this, Right-click on Project->Properties->Build->Check Prefer 32-bit checkbox. Thanks all

0
  1. Delete the temp test directory located here C:\Users(User)\AppData\Local\Temp\VisualStudioTestExplorerExtensions\

  2. Set all projects to x64 in Visual Studio

  3. Set the default processor architecture to x64(Test/TestSettings/Default Processor Architecture).

Make sure to clean build the solution file.Hope this helps!

0

Changing the Target Framework in project properties from .NET Framework 4.7.1 to 4.6.2 worked for me.

Not the answer you're looking for? Browse other questions tagged or ask your own question.