.NET Core 3.1 控制台应用程序不会 运行 Windows 7

.NET Core 3.1 Console App will not run on Windows 7

我使用 Visual Studio 2019 和最新补丁创建了一个 .NET Core 3.1 AnyCpu 控制台应用程序。它包含的唯一代码是创建它时使用的样板 Console.WriteLine("Hello World!")。我编译了它,它 运行 在我的 Windows 10 x64 盒子上很好。

我将 Debug 文件夹复制到我的 32 位 Windows 7 框并尝试 运行 控制台应用程序。我收到以下消息。 (.Net Core 3.1 运行时间安装在Windows 7盒子上。

The version of this file is not compatible with the version of Windows you're running. Check your computer's system information to see whether you need an x86 (32-bit) or x64 (64-bit) version of the program, and then contact the software publisher.

但是,如果我使用 x86 编译控制台应用程序,那么它 运行 在 Windows 7 框上没问题。使用 VS2019 和 .NET Core 3/3.1 AnyCpu 改变了吗?我原以为为 AnyCpu 编译的代码应该在 32 位和 64 位下运行良好。

看起来 AnyCpu 在 .Net Core 3 中的工作方式发生了变化。在 .NET Framework 中,.exe 是一个托管的 exe,因此对于 AnyCpu,它在 运行 时间进行 JIT 以无论需要 x86 还是 x64 **。在使用 AnyCpu 的 .Net Core 中,.Exe 是一个普通的非托管 exe,针对 x86 / x64 编译,具体取决于您的机器进行编译的架构。但是,它创建的 dll 包含托管代码,并且可以 运行 在 32 位计算机上使用 DotNet.exe.

解决方案不是在 32 位机器上编译,因为这只会在 32 位模式下启动 dll。解决方案实际上是不使用 .exe 并使用 DotNet.exe.

的本地副本启动 dll

** 来源:https://mihai-albert.com/2020/03/08/startup-sequence-of-a-dotnet-core-app/