dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.68k stars 1.06k forks source link

Could not load file or assembly 'System.Windows.Forms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in .Net9-rc1 #43593

Closed joesdu closed 4 days ago

joesdu commented 3 weeks ago

I used WPF to develop a program, in .NET8 when everything was fine, but when I switched to .NET9-RC1, if in the open <SelfContained>true</SelfContained> configuration, and then use VS to press F5 to start the program will throw an exception, and found that it is the lack of these dynamic link libraries in the figure.

bc1e43e0adaf3b30c31269f44645c24

The problem can be reproduced using this program

Describe the bug

A clear and concise description of what the bug is.

To Reproduce

Exceptions (if any)

ReflectionTypeLoadException: Unable to load one or more of the requested types. Could not load file or assembly 'System.Windows.Forms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. 系统找不到指定的文件。 Could not load file or assembly 'System.Windows.Forms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. 系统找不到指定的文件。 Could not load file or assembly 'System.Windows.Forms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. 系统找不到指定的文件。

Further technical details

C:\Users\Joe>dotnet --info .NET SDK: Version: 9.0.100-rc.1.24452.12 Commit: 81a714c6d3 Workload version: 9.0.100-manifests.67cd1eb6 MSBuild version: 17.12.0-preview-24422-09+d17ec720d

运行时环境: OS Name: Windows OS Version: 10.0.27695 OS Platform: Windows RID: win-x64 Base Path: C:\Program Files\dotnet\sdk\9.0.100-rc.1.24452.12\

已安装 .NET 工作负载: 配置为在安装新清单时使用 loose manifests。 没有要显示的已安装工作负载。

Host: Version: 9.0.0-rc.1.24431.7 Architecture: x64 Commit: static

.NET SDKs installed: 8.0.400 [C:\Program Files\dotnet\sdk] 9.0.100-rc.1.24452.12 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 9.0.0-rc.1.24452.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 9.0.0-rc.1.24431.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 9.0.0-rc.1.24452.1 [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

Learn more: https://aka.ms/dotnet/info

Download .NET: https://aka.ms/dotnet/download

dotnet-issue-labeler[bot] commented 3 weeks ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

dotnet-issue-labeler[bot] commented 3 weeks ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

huoyaoyuan commented 2 weeks ago

This file may be a virus program, please do not download it. 该文件可能是一个病毒程序,请不要下载.

Note: do not directly quote reply spam comment. The link will still be present after the comment is deleted by GitHub.

huoyaoyuan commented 2 weeks ago

System.Windows.Forms is WinForm, not WPF. It' expected to not be included when you specify UseWPF only.

The problem is what's referencing WinForm. Can you include a stack trace for the exception?

joesdu commented 1 week ago

System.Windows.Forms is WinForm, not WPF. It' expected to not be included when you specify UseWPF only.

The problem is what's referencing WinForm. Can you include a stack trace for the exception?

I have used WPF components throughout and did not include any WinForm references, WinForm components, or any WinForm-related code. You can download the demo I provided to verify this. The WinForm library reference might be caused by the underlying WPF dependencies.

huoyaoyuan commented 1 week ago

The WinForm library reference might be caused by the underlying WPF dependencies.

That's why stack trace helps. It can identify what is trying to load WinForms.

joesdu commented 1 week ago

The WinForm library reference might be caused by the underlying WPF dependencies.

That's why stack trace helps. It can identify what is trying to load WinForms.

在 System.Reflection.RuntimeModule.GetDefinedTypes() 在 System.Reflection.RuntimeModule.GetTypes() 在 System.Linq.Enumerable.SelectManySingleSelectorIterator2.MoveNext() 在 System.Linq.Enumerable.IEnumerableWhereIterator1.MoveNext() 在 EasilyNET.AutoDependencyInjection.Modules.ModuleApplicationBase.d23.MoveNext() 在 System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable1 source, Func2 predicate, Boolean& found) 在 System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable1 source, Func2 predicate) 在 EasilyNET.AutoDependencyInjection.Modules.ModuleApplicationBase.get_LoadModules() 在 EasilyNET.AutoDependencyInjection.Modules.ModuleApplicationBase..ctor(Type startupModuleType, IServiceCollection services) 在 EasilyNET.AutoDependencyInjection.Modules.StartupModuleRunner..ctor(Type startupModuleType, IServiceCollection services) 在 EasilyNET.AutoDependencyInjection.ApplicationFactory.Create[TStartupModule](IServiceCollection services) 在 Microsoft.Extensions.DependencyInjection.ServiceCollectionExtension.AddApplicationModules[T](IServiceCollection services) 在 WpfAutoDISample.Program.<>c.b__4_2(IServiceCollection sc) 在 G:\GitHub\WpfAutoDISample\WpfAutoDISample\Program.cs 中: 第 128 行 在 Microsoft.Extensions.Hosting.HostBuilder.InitializeServiceProvider() 在 Microsoft.Extensions.Hosting.HostBuilder.Build() 在 WpfAutoDISample.Program.<>cDisplayClass2_0.b__0() 在 G:\GitHub\WpfAutoDISample\WpfAutoDISample\Program.cs 中: 第 65 行 在 System.Threading.Tasks.Task`1.InnerInvoke() 在 System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)

huoyaoyuan commented 6 days ago

I've debugged this a bit. To diagnose issues with publishing, you can set Debugger.Launch at the entry point, then run the published application under Debug configuration. I also used ProjectReference to EasyNET and changed the throwing lazy LINQ query to eager foreach.

The exception happens when loading types of PresentationUI, which has dependency on System.Windows.Forms. I'm not sure how it get dropped during trimming, but since WPF still doesn't claim trimming compatibility, I'm afraid you are touching unsupported case.

I see you are trying to exclude framework assemblies at https://github.com/joesdu/EasilyNET/blob/a0484e0f4d07f8d10967bfa86dfdb866d2611eab/src/EasilyNET.Core/Misc/AssemblyHelper.cs#L16 . It doesn't cover some WPF assemblies starting with PresentationFramework, PresentationUI etc. Another trick is to check public key token of assemblies. There are several public keys used by framework assemblies and you can collect all of them.

joesdu commented 4 days ago

I've debugged this a bit. To diagnose issues with publishing, you can set Debugger.Launch at the entry point, then run the published application under Debug configuration. I also used ProjectReference to EasyNET and changed the throwing lazy LINQ query to eager foreach.

The exception happens when loading types of PresentationUI, which has dependency on System.Windows.Forms. I'm not sure how it get dropped during trimming, but since WPF still doesn't claim trimming compatibility, I'm afraid you are touching unsupported case.

I see you are trying to exclude framework assemblies at https://github.com/joesdu/EasilyNET/blob/a0484e0f4d07f8d10967bfa86dfdb866d2611eab/src/EasilyNET.Core/Misc/AssemblyHelper.cs#L16 . It doesn't cover some WPF assemblies starting with PresentationFramework, PresentationUI etc. Another trick is to check public key token of assemblies. There are several public keys used by framework assemblies and you can collect all of them.

Thx, fixed in 3.24.1007.185