dotnet / dotnet/wpf

wpf throw Could not load file or assembly 'System.Windows.Forms exception on starting if published with --self-contained

Open
#10,108 4 comments 2 reactions 0 assignees View on GitHub
Investigate
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

When I run the wpf application it throw
Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.
if any third party need System.Windows.Forms when I publish the application using --self-contained and works fine if --self-contained=false

### Reproduction Steps

Create a new wpf project and references it for example Microsoft.AppCenter which requires System.Windows.Forms

configure the AppCenter on startup

```
public partial class App : Application
{
public App()
{
Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
}

private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show(e.Exception.Message);
}

protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
SetAppCenter();
}
private void SetAppCenter()
{
Microsoft.AppCenter.AppCenter.Start("null",
typeof(Microsoft.AppCenter.Analytics.Analytics), typeof(Crashes));

}

}

```

### Expected behavior

To run normally like self-contained=false publishing

### Actual behavior

It throws exception
Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.

### Regression?

It works if I publish the using self-contained=false

### Known Workarounds

_No response_

### Impact

_No response_

### Configuration

dotnet 9
windows 11
x64

### Other information

[WpfApp9-master.zip](https://github.com/user-attachments/files/17876372/WpfApp9-master.zip)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.