经验分享 · 2025年3月25日

DotNetCore 桌面应用(包括NativeAOT发布)在Windows 7 SP1 下不能运行的问题的解决方法

笔者遇到的问题:.net 9.0 开发的桌面应用,在Win7 x64 sp1上无法运行,事件查看器可见错误:

AOT发布模式下日志:

Exception Info: System.TypeInitializationException: A type initializer threw an exception. To determine which type, inspect the InnerException’s StackTrace property.
at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0xf3
at System.Runtime.CompilerServices.ClassConstructorRunner.CheckStaticClassConstructionReturnGCStaticBase(StaticClassConstructionContext*, Object) + 0x8
at System.Threading.TimerQueueTimer..ctor(TimerCallback, Object, UInt32, UInt32, Boolean) + 0xf4
at System.Threading.Timer.TimerSetup(TimerCallback, Object, UInt32, UInt32, Boolean) + 0x31
at System.Threading.Timer..ctor(TimerCallback, Object, TimeSpan, TimeSpan) + 0x99
at Avalonia.Rendering.DefaultRenderTimer.StartCore(Action1) + 0x8d
at Avalonia.Rendering.DefaultRenderTimer.Start() + 0x47
at Avalonia.Rendering.DefaultRenderTimer.add_Tick(Action
1) + 0x51
at Avalonia.Rendering.RenderLoop.Add(IRenderLoopTask) + 0xda
at Avalonia.Rendering.Composition.Server.ServerCompositor..ctor(IRenderLoop, IPlatformGraphics, CompositionOptions, BatchStreamObjectPool1, BatchStreamMemoryPool) + 0x215
at Avalonia.Rendering.Composition.Compositor..ctor(IRenderLoop, IPlatformGraphics, Boolean, ICompositorScheduler, Boolean, Dispatcher, CompositionOptions) + 0x1e8
at Avalonia.Win32.Win32Platform.Initialize(Win32PlatformOptions) + 0x498
at Avalonia.Win32ApplicationExtensions.<>c.<UseWin32>b__0_0() + 0x4d
at Avalonia.AppBuilder.SetupUnsafe() + 0x3c
at Avalonia.AppBuilder.Setup() + 0x39
at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime(AppBuilder, String[], Action
1) + 0x27
at FAControlsGallery.Desktop.Program.Main(String[] args) + 0x16

安装DesktopRuntime,运行普通编译

Message: Failed to load the dll from [C:\Users\ahe\Desktop\ava\FluentAvalonia.Desktop\hostfxr.dll], HRESULT: 0x80070057
The library hostfxr.dll was found, but loading it from C:\Users\ahe\Desktop\ava\FluentAvalonia.Desktop\hostfxr.dll failed

安装对应架构的KB3063858后可正常运行

遇到的问题#

一个小的桌面应用,发布到Windows 7 SP1 x86系统,打包成Self-Contained进行发布的时候,运行后没反应,也不报错。打包成SingleFile之后,运行报错 api-ms-win-crt-runtime-l1-1-0.dll 缺失。之后尝试安装.Net Core 3.1的Desktop Runtime,问题依旧。然后又搜索一阵,大概是Windows 7 SP1系统里面,正常运行 .Net Core 3.1的应用,需要额外安装两个软件包:

  • Microsoft Visual C++ 2015-2019 Redistributable 64 位 / 32 位
  • KB3063858 64 位 / 32 位
  • Microsoft 根证书颁发机构 2011(仅限 .NET Core 2.1 脱机安装程序)【安装上面两个之后,程序正常运行了,这个没管它】

安装完之后,再运行就没问题了😂

参考资料#

Windows 7 安装 .NET 5 / .NET Core 3.1 环境的方法和依赖文件
https://www.coderbusy.com/archives/798.html

探索 dotnet core 为何在 Windows7 系统需要补丁的原因
https://www.cnblogs.com/lindexi/p/15768396.html

官方文档
https://learn.microsoft.com/zh-cn/dotnet/core/install/windows?tabs=netcore31#windows-7–vista–81–server-2008-r2–server-2012-r2

软件包下载#

Microsoft Visual C++ Redistributable for Visual Studio 2022
https://visualstudio.microsoft.com/zh-hans/downloads/
https://aka.ms/vs/17/release/VC_redist.x86.exe
https://aka.ms/vs/17/release/VC_redist.x64.exe

Microsoft Visual C++ Redistributable for Visual Studio 2019
https://aka.ms/vs/16/release/vc_redist.x64.exe
https://aka.ms/vs/16/release/vc_redist.x86.exe

系统更新KB3063858
64位
https://www.microsoft.com/download/details.aspx?id=47442
https://download.microsoft.com/download/0/8/E/08E0386B-F6AF-4651-8D1B-C0A95D2731F0/Windows6.1-KB3063858-x64.msu

32位
https://www.microsoft.com/download/details.aspx?id=47409
https://download.microsoft.com/download/C/9/6/C96CD606-3E05-4E1C-B201-51211AE80B1E/Windows6.1-KB3063858-x86.msu