从 .NET Framework 移植到 .NET Core 的过程概述Overview of the porting process from .NET Framework to .NET Core

你可能有些代码当前正在 .NET Framework 上运行,但你想将这些代码移植到 .NET Core。You might have code that currently runs on the .NET Framework that you're interested in porting to .NET Core. 本文将提供:This article provides:

  • 移植过程概述。An overview of the porting process.
  • 在将代码移植到 .NET Core 时,可能会发现一系列有用的工具。A list of the tools you may find helpful when you're porting your code to .NET Core.

移植过程概述Overview of the porting process

我们建议在将项目移植到 .NET Core 时使用以下过程:We recommend you to use the following process when porting your project to .NET Core:

  1. 将希望移植的所有项目重定向到目标 .NET Framework 4.7.2 或更高版本。Retarget all projects you wish to port to target the .NET Framework 4.7.2 or higher.

    此步骤可确保在 .NET Core 不支持特殊 API 的情况下,可以为特定于 .NET Framework 的目标使用备用 API。This step ensures that you can use API alternatives for .NET Framework-specific targets when .NET Core doesn't support a particular API.

  2. 使用 .NET 可移植性分析器来分析程序集,并查看这些程序集是否可移植到 .NET Core。Use the .NET Portability Analyzer to analyze your assemblies and see if they're portable to .NET Core.

    API 可移植性分析器工具可分析已编译的程序集并生成报表。The API Portability Analyzer tool analyzes your compiled assemblies and generates a report. 此报表显示高级别可移植性摘要,以及你所使用的不适用于 NET Core 的各个 API 细目。This report shows a high-level portability summary and a breakdown of each API you're using that isn't available on NET Core.

  3. .NET API 分析器安装到项目中,以识别在某些平台上会引发 PlatformNotSupportedException 的 API 以及一些其他潜在的兼容性问题。Install the .NET API analyzer into your projects to identify APIs throwing PlatformNotSupportedException on some platforms and some other potential compatibility issues.

    此工具与可移植性分析器类似,但它不会分析是否可以在 .NET Core 上进行构建,而是分析你是否正在以会导致在运行时引发 PlatformNotSupportedException 的某种方式使用 API。This tool is similar to the portability analyzer, but instead of analyzing if things can build on .NET Core, it will analyze if you're using an API in a way that will throw the PlatformNotSupportedException at runtime. 尽管这并不常见,但如果从 .NET Framework 4.7.2 或更高版本进行移动,最好进行检查。Although this isn't common if you're moving from .NET Framework 4.7.2 or higher, it's good to check.

  4. 通过 Visual Studio 中的转换工具将所有 packages.config 依赖项转换为 PackageReference 格式。Convert all of your packages.config dependencies to the PackageReference format with the conversion tool in Visual Studio.

    此步骤涉及到转换旧 packages.config 格式的依赖项。This step involves converting your dependencies from the legacy packages.config format. packages.config 不适用于 .NET Core,因此,如果你有包依赖项,则需要进行此转换。packages.config doesn't work on .NET Core, so this conversion is required if you have package dependencies.

  5. 为 .NET Core 创建新项目并覆盖源文件,或尝试使用工具转换现有的项目文件。Create new projects for .NET Core and copy over source files, or attempt to convert your existing project file with a tool.

    .NET Core 使用不同于 .NET Framework 的更简化的项目文件格式.NET Core uses a simplified (and different) project file format than .NET Framework. 需要将项目文件转换为此格式才能继续操作。You'll need to convert your project files into this format to continue.

  6. 移植测试代码。Port your test code.

    由于移植到 .NET Core 对代码库来说是很大的更改,因此强烈建议移植测试代码,以便在移植代码时可以进行测试。Because porting to .NET Core is such a significant change to your codebase, it's highly recommended to get your tests ported, so that you can run tests as you port your code over. MSTest、xUnit 和 NUnit 都适用于 .NET Core。MSTest, xUnit, and NUnit all work on .NET Core.

此外,在某个操作中,可以尝试使用 dotnet try-convert 工具将较小的解决方案或单个项目移植到 .NET Core 项目文件格式。Additionally, you can attempt to port smaller solutions or individual projects to the .NET Core project file format with the dotnet try-convert tool in one operation. 不能保证 dotnet try-convert 适用于所有项目,而且它可能会导致所依赖的行为发生细微变化。dotnet try-convert is not guaranteed to work for all your projects, and it may cause subtle changes in behavior that you may find that you depended on. 应将它用作一个起点 ,以自动化可自动执行的基本操作。It should be used as a starting point that automates the basic things that can be automated. 作为用于迁移项目的一种解决方案,它并不是万无一失的。It isn't a guaranteed solution to migrating a project.