3
votes

"Could not load file or assembly 'Microsoft.ServiceFabric.Data' or one of its dependencies. An attempt was made to load a program with an incorrect format."

Exception Details: System.BadImageFormatException: Could not load file or assembly 'Microsoft.ServiceFabric.Data' or one of its dependencies. An attempt was made to load a program with an incorrect format.

I added nuget of Service Fabric to my web api and here's the exception. I tried to complie x64 / AnyCPU / x86

Any help?

4

4 Answers

9
votes

The answer for this question solved this issue for me

I get a "An attempt was made to load a program with an incorrect format" error on a SQL Server replication project

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".

5
votes

I faced the same issue, both for web applications and when running tests.

When running a web application : : System.BadImageFormatException : "Could not load file or assembly 'Microsoft.ServiceFabric.Data' or one of its dependencies. An attempt was made to load a program with an incorrect format."

Possible cause: your IIS is not setup to run in a 64 bit version. Service Fabric assemblies only run on 64 bit platforms.

Solution: In that case you need to do (as suggested above) in Visual Studio TOOLS > OPTIONS > Projects and Solutions > WEB PROJECTS and check the "Use the 64 bit version of IIS Express for web sites and projects".

When running a test : System.BadImageFormatException : "Could not load file or assembly 'Microsoft.ServiceFabric.Data' or one of its dependencies. An attempt was made to load a program with an incorrect format."

Possible cause: your test runner is not setup to run in a 64 bit version. Service Fabric assemblies only run on 64 bit platforms.

Solution: Change the test runner settings in Visual Studio: Test menu > Test Settings > Default Processor Architecture > Set to x64.

Also make sure that all your projects are configured to run on x64 platforms, not x86. You can ensure this in the Configuration Manager of your solution, make sure you do not have any active profile for x86.

2
votes

I had the same problem and except the steps shown in previous answer (TOOLS > OPTIONS > Projects and Solutions > WEB PROJECTS and check the "Use the 64 bit version), I had to go to my project Properties -> Build -> and set up 'Platform targed:' to x64.

1
votes

This happened to me before. Somehow during the install, it did not copy some of the files which I don't know why. All I did was reinstall the SDK again. I think stopping the SF service and making sure I don't have any VS running helps.