使用本机映像优化 NET 桌面应用Optimize your .NET desktop apps with native images

可以通过预编译二进制文件来改善 .NET Framework 应用程序的启动时间。You can improve the startup time of your .NET Framework application by pre-compiling your binaries. 可以针对通过 Microsoft Store 打包和分发的大型应用程序使用此技术。You can use this technology on large applications that you package and distribute through the Microsoft Store. 我们已观察到,在某些情况下,这样可以将性能提升 20%。In some cases, we've observed a 20% performance improvement. 可以在技术概述中详细了解此技术。You can learn more about this technology in the technical overview.

我们以 NuGet 包的形式发布了本机映像编译器。We've released the native image compiler as a NuGet package. 可将此包应用到面向 .NET Framework 4.6.2 或更高版本的任何 .NET Framework 应用程序。You can apply this package to any .NET Framework application that targets the .NET Framework version 4.6.2 or later. 此包会将一个包含本机有效负载的生成后步骤添加到应用程序使用的所有二进制文件。This package adds a post build step that includes a native payload to all the binaries used by your application. 当应用程序在 .NET 4.7.2 和更高版本中运行时,将加载这个优化的有效负载,而旧版本仍然加载 MSIL 代码。This optimized payload will be loaded when the application runs in .NET 4.7.2 and above while previous versions will still load the MSIL code.

Windows 10 2018 年 4 月更新中包含了 .NET framework 4.7.2The .NET framework 4.7.2 is included in the Windows 10 April 2018 update. 还可以在运行 Windows 7+ 和 Windows Server 2008 R2+ 的电脑上安装此版本的 .NET Framework。You can also install this version of the .NET Framework on PCs that run Windows 7+ and Windows Server 2008 R2+.

重要

若要为 Windows 应用程序打包项目打包的应用程序生成本机映像,请确保将项目的“目标平台最低版本”设置为“Windows 周年更新”。If you want to produce native images for your application packaged by the Windows Application Packaging project, make sure to set the Target Platform Minimum version of the project to the Windows Anniversary Update.

如何生成本机映像How to produce native images

按照以下说明配置项目。Follow these instructions to configure your projects.

  1. 将目标框架配置为 4.6.2 或以上Configure the target framework as 4.6.2 or above

  2. 将目标平台配置为 x86 或 x64Configure the target platform as x86 or x64

  3. 添加 NuGet 包。Add the NuGet packages.

  4. 创建发布版本。Create a Release Build.

将目标框架配置为 4.6.2 或以上Configure the target framework as 4.6.2 or above

若要将项目配置为面向 .NET Framework 4.6.2,需要安装 .NET Framework 4.6.2 或更高版本的开发工具。To configure your project to target .NET Framework 4.6.2 you will need the .NET Framework 4.6.2 development tools or newer. 在 .NET 桌面开发工作负荷中,这些工具以可选组件的形式通过 Visual Studio 安装程序提供:These tools are available through the Visual Studio installer as optional components under the .NET desktop development workload:

安装 .NET 4.6.2 开发工具

或者,可以从 https://www.microsoft.com/net/download/visual-studio-sdks 获取 .NET 开发人员包Alternatively, you can get the .NET developer packs from: https://www.microsoft.com/net/download/visual-studio-sdks

将目标平台配置为 x86 或 x64Configure the target platform as x86 or x64

本机映像编译器优化给定平台的代码。The native image compiler optimizes the code for a given platform. 若要使用此工具,需将应用程序配置为面向某个特定平台,例如 x86 或 x64。To use it, you need to configure your application to target one specific platform such as x86 or x64.

如果解决方案中包含多个项目,只能将入口点项目(很有可能是生成可执行文件的项目)编译为 x86 或 x64。If you have multiple projects in your solution, only the entry point project (most likely the project that produces an executable file) has to be compiled as x86 or x64. 从主项目引用的其他二进制文件将使用主项目中指定的体系结构进行处理,即使这些二进制文件编译为 AnyCPU。Additional binaries referenced from the main project will be processed with the architecture specified in the main project, even if they are compiled as AnyCPU.

若要配置项目:To configure your project:

  1. 右键单击解决方案并选择“配置管理器”。 Right-click your solution, and then select Configuration Manager.

  2. 在生成可执行文件的项目名称旁边的“平台”下拉菜单中,选择“<新建...>”。 Select <New ..> in the Platform dropdown menu beside the name of the project that produces your executable file.

  3. 在“新建项目平台”对话框中,确保“从此处复制设置”下拉列表设置为“任何 CPU”。 In the New Project Platform dialog box, make sure that the Copy Settings from dropdown list is set to Any CPU.

配置 x86

若要生成 x64 二进制文件,请对 Release/x64 重复此步骤。Repeat this step for Release/x64 if you want produce x64 binaries.

重要

本机映像编译器不支持“任何 CPU”配置。AnyCPU configuration is not supported by the native image compiler.

添加 NuGet 包Add the NuGet packages

本机映像编译器以 NuGet 包的形式提供,需将其添加到生成可执行文件的 Visual Studio 项目中。The native image compiler is provided as a NuGet package that you need to add to the Visual Studio project that produces the executable file. 此项目通常是 Windows 窗体或 WPF 项目。This is typically your Windows Forms or WPF project. 使用以下 PowerShell 命令执行此操作。Use this PowerShell command to do that.

PM> Install-Package Microsoft.DotNet.Framework.NativeImageCompiler -Version 1.0.0

创建发布版本Create a Release Build

NuGet 包将项目配置为针对发布版本运行一个附加的工具。The NuGet package configures the project to run an additional tool for release builds. 此工具将本机代码添加到相同的二进制文件。This tool adds the native code to the same binaries. 若要验证该工具是否已处理二进制文件,可以查看生成输出,确保其中包含如下所示的消息:To verify that the tool has processed the binaries you can review the build output to make sure it includes a message such as this one:

Native image obj\x86\Release\\R2R\DesktopApp1.exe generated successfully.

可以通过在项目文件中将属性 NgenR2R 设置为 true,对非发布版本触发本机映像编译。Native image compilation can be triggered on non-release builds by setting the property NgenR2R to true in the project file.

FAQFAQ

问:在没有 .NET Framework 4.7.2 的情况下,新二进制文件是否可在计算机上正常运行?Q. Do the new binaries work on machines without .NET Framework 4.7.2?

A.A. 在装有 .NET Framework 4.7.2 的情况下运行时,优化的二进制文件可以从改进的功能中受益。Optimized binaries will benefit from the improvements when running with .NET Framework 4.7.2. 运行旧版 .NET Framework 的客户端将从二进制文件中加载未优化的 MSIL 代码。Clients that run previous .NET framework versions will load the non-optimized MSIL code from the binary.

问:如何提供反馈或报告问题?Q. How can I provide feedback or report issues?

A.A. 请使用 Visual Studio 2017 中的“反馈”工具报告问题。Report an issue by using the Feedback tool in Visual Studio 2017. 详细信息More information.

问:将本机映像添加到现有二进制文件会造成什么影响?Q. What’s the impact of adding the native image to existing binaries?

A.A. 优化的二进制文件包含托管代码和本机代码,使最终文件变得更大。The optimized binaries contain the managed and native code, making the final files greater.

问:是否可以使用此技术发布二进制文件?Q. Can I release binaries using this technology?

A.A. 此版本包含立即可供使用的“上线”许可证。This version includes a Go Live license that you can use today.