dotnet / dotnet/sdk

BadImageFormatException referencing an x64 assembly in a "single file" / "self contained" application

Open
#46,984 10 comments 0 reactions 0 assignees View on GitHub
area-Single-File
Dominant language
C#
Stars
3.2k
Forks
1.3k
PR merge metrics
PR metrics pending

Description

### Describe the bug
I have a .NET Core CLI app that uses the Microsoft Flight Simulator SDK. This is implemented as a .NET Framework x64 format assembly. If I add the x64 format assembly to my x64 app, it will build and run fine, until I try and use the "single file" / "self contained" publishing to create a single executable. If I do that, I receive a `System.BadImageFormatException` on loading the referenced assembly.

### To Reproduce

https://github.com/ngreatorex/DotNetSelfContainedBadImageFormatExceptionRepro

You shouldn't need Flight Simulator installed to run the example. The expected output of the application without the simulator #would be
```
Failed to connect to Flight Simulator: Unexpected HRESULT has been returned from a call to a COM component.
```
If you run the app with `dotnet run` you should get the above output.
If you publish the app with `dotnet publish` and then run ` .\bin\Release\net9.0\win-x64\publish\SelfContainedExample.exe` you get the exception below.

### Exceptions (if any)

```
Unhandled exception. System.BadImageFormatException:
File name: 'C:\Users\\SelfContainedExample\SelfContainedExample\bin\x64\Debug\net9.0\win-x64\publish\Microsoft.FlightSimulator.SimConnect.dll'
at Program.$(String[] args)
```

### Further technical details
Tried both in Visual Studio 2022 Community (17.13.1) and from the dotnet CLI (9.0.200)

dotnet --info

```
.NET SDK:
Version: 9.0.200
Commit: 90e8b202f2
Workload version: 9.0.200-manifests.69179adf
MSBuild version: 17.13.8+cbc39bea8

Runtime Environment:
OS Name: Windows
OS Version: 10.0.26100
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\9.0.200\

.NET workloads installed:
[android]
Installation Source: SDK 9.0.200, VS 17.14.35806.103, VS 17.13.35818.85
Manifest Version: 35.0.39/9.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\9.0.100\microsoft.net.sdk.android\35.0.39\WorkloadManifest.json
Install Type: Msi

[aspire]
Installation Source: SDK 9.0.200, VS 17.14.35806.103, VS 17.13.35818.85
Manifest Version: 8.2.2/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.aspire\8.2.2\WorkloadManifest.json
Install Type: Msi

[ios]
Installation Source: SDK 9.0.200, VS 17.14.35806.103, VS 17.13.35818.85
Manifest Version: 18.2.9180/9.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\9.0.100\microsoft.net.sdk.ios\18.2.9180\WorkloadManifest.json
Install Type: Msi

[maccatalyst]
Installation Source: SDK 9.0.200, VS 17.14.35806.103, VS 17.13.35818.85
Manifest Version: 18.2.9180/9.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\9.0.100\microsoft.net.sdk.maccatalyst\18.2.9180\WorkloadManifest.json
Install Type: Msi

[maui-windows]
Installation Source: SDK 9.0.200, VS 17.14.35806.103, VS 17.13.35818.85
Manifest Version: 9.0.14/9.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\9.0.100\microsoft.net.sdk.maui\9.0.14\WorkloadManifest.json
Install Type: Msi

[wasm-tools]
Installation Source: SDK 9.0.200, VS 17.13.35818.85
Manifest Version: 9.0.2/9.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\9.0.100\microsoft.net.workload.mono.toolchain.current\9.0.2\WorkloadManifest.json
Install Type: Msi

Configured to use loose manifests when installing new manifests.

Host:
Version: 9.0.2
Architecture: x64
Commit: 80aa709f5d

.NET SDKs installed:
6.0.428 [C:\Program Files\dotnet\sdk]
9.0.200 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.All 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
Not set

global.json file:
Not found
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.