Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
alraseensaad opened this issue Nov 22, 2024 · 4 comments
Labels
Investigate Requires further investigation by the WPF team.

Comments

@alraseensaad
Copy link

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

@lindexi
Copy link
Member

lindexi commented Nov 25, 2024

@alraseensaad It is the Microsoft.AppCenter 's issues, not wpf issues. Because the Microsoft.AppCenter reference the System.Windows.Forms, Version=4.0.0.0 assembly which cause this exception.

The main break code is in ApplicationLifecycleHelperDesktop cctor:

	static ApplicationLifecycleHelperDesktop()
	{
		... 
		System.Windows.Forms.Application.ApplicationExit += delegate
		{
			WindowsHelper.OnMinimized -= _OnMinimizedDelegate;
		};
	}

And you do not add the UseWindowsForms to csproj, which will ignore the System.Windows.Forms.dll file when publish with self contained.

How to fix?

Adding the <UseWindowsForms>true</UseWindowsForms> to csproj or fix the Microsoft.AppCenter NuGet package.

@himgoyalmicro
Copy link
Contributor

@alraseensaad Does @lindexi suggestion solves your issue ?

@himgoyalmicro himgoyalmicro added the 📭 waiting-author-feedback To request more information from author. label Nov 25, 2024
@alraseensaad
Copy link
Author

@lindexi

I just give the Microsoft.AppCenter as examples to reproduce the problem but it does not work for any third-party that depend on Windows Form and the problem is that as it self contained it show includes all needed references as it was before dotnet 9

Also it works in dotnet 8 without using <UseWindowsForms>true</elf contained>

and My project is Wpf why to UseWindowsForms which will show all WindowsForms staff on the development environment

@dotnet-policy-service dotnet-policy-service bot removed the 📭 waiting-author-feedback To request more information from author. label Nov 26, 2024
@lindexi
Copy link
Member

lindexi commented Nov 27, 2024

@alraseensaad The main reason is the builder will not output WinFroms.dll when its UseWindowsForms reference is not set in dotnet 9

@himgoyalmicro himgoyalmicro added the Investigate Requires further investigation by the WPF team. label Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Investigate Requires further investigation by the WPF team.
Projects
None yet
Development

No branches or pull requests

3 participants