BadImageFormatException when PlatformTarget architecture is different than current SDK
See original GitHub issueSteps to reproduce
- Install x86 and x64 .NET Core SDKs
- Create new dotnet console app (
dotnet new console
) - Set the
PlatformTarget
in explicit way to architecture different than your current SDK dotnet run
using System;
namespace report
{
class Program
{
static void Main(string[] args) => Console.WriteLine($"IntPtr.Size={IntPtr.Size}");
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
</Project>
Expected behavior
dotnetcli runs the program for given platform target
Actual behavior
Program crashes:
Unhandled Exception: System.BadImageFormatException: Could not load file or assembly 'C:\temp\report\bin\Debug\netcoreapp2.0\report.dll'. An attempt was made to load a program with an incorrect format.
Environment data
dotnet --info
output:
.NET Command Line Tools (2.1.0-preview1-007074)
Product Information:
Version: 2.1.0-preview1-007074
Commit SHA-1 hash: ee5c02de77
Runtime Environment:
OS Name: Windows
OS Version: 10.0.14393
OS Platform: Windows
RID: win10-x86
Base Path: C:\Program Files (x86)\dotnet\sdk\2.1.0-preview1-007074\
Microsoft .NET Core Shared Framework Host
Version : 2.1.0-preview2-25615-02
Build : 2d818e87081bfe71f9a3f2cbba43cb279e79b265
Why do I need that
In BenchmarkDotNet we generate, build and run new project for every benchmark. We use dotnet cli to do that. I would like to allow our users to compare the performance of x64 vs x86 with single config. I can imagine that dotnet cli team is busy. Could you at least give me some workaround?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:8
- Comments:16 (7 by maintainers)
Top Results From Across the Web
BadImageFormatException when PlatformTarget ...
Steps to reproduce Install x86 and x64 .NET Core SDKs Create new dotnet console app (dotnet new console) Set the PlatformTarget in explicit ......
Read more >c# - BadImageFormatException x64 Issue
For me, the solution was to go to the service project properties, and change the build platform to "Any CPU", and also uncheck...
Read more >BadImageFormatException Class (System)
The exception that is thrown when the file image of a dynamic link library (DLL) or an executable program is invalid.
Read more >C# Target Platforms | x64 vs x86 vs AnyCPU
Assemblies compiled as x86 will run as a 32-bit process and can load Any CPU and x86 assemblies but not x64 (BadImageFormatException) ....
Read more >System.BadImageFormatException - Autodesk.Navisworks. ...
You're building your project targeting a different architecture than one of your references, Autodesk.Navisworks.Automation.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
By default, VS (and dotnet run) will just use the dotnet.exe that comes first on your PATH to run the application. If that is the x64 version of dotnet.exe, then you will not be able to debug x86 apps, and vice versa.
However, you can override the default run command. I suggest the following approach:
What this does is the following:
I am leaving this open to track making this work automatically without having to customize the RunCommand.
With the change to use an apphost by default, this now comes down to duplicate of dotnet/sdk#2632. We now put the exe that hosts the app into the app folder, so once we put the right one there, we do not need to leak in ProgramFiles (x86 or otherwise) for the appropriate host. The apphost will then find the right shared framework itself.
Closing in favor of dotnet/sdk#2632, this experience should get fixed in 3.0. 🎉 cc @peterhuene