从版本 2.0 迁移到 2.1 的中断性变更Breaking changes for migration from version 2.0 to 2.1

若要从 .NET Core 的版本 2.0 迁移到 2.1,本文中列出的中断性变更可能会影响你的应用。If you're migrating from version 2.0 to version 2.1 of .NET Core, the breaking changes listed in this article may affect your app.

Core .NET 库Core .NET libraries

添加到内置结构类型的私有字段Private fields added to built-in struct types

私有字段已添加到引用程序集中的特定结构类型Private fields were added to certain struct types in reference assemblies. 因此,在 C# 中,必须始终使用 new 运算符默认文本来实例化结构类型。As a result, in C#, those struct types must always be instantiated by using the new operator or default literal.

更改描述Change description

在 .NET Core 2.0 和早期版本中,某些提供的结构类型(例如 ConsoleKeyInfo)可以在不使用 new 运算符或默认文本的情况下在 C# 中实例化。In .NET Core 2.0 and previous versions, some provided struct types, for example, ConsoleKeyInfo, could be instantiated without using the new operator or default literal in C#. 这是因为 C# 编译器使用的引用程序集不包含结构的私有字段。This was because the reference assemblies used by the C# compiler didn't contain the private fields for the structs. 从 .NET Core 2.1 开始,.NET 结构类型的所有私有字段都将添加到引用程序集。All private fields for .NET struct types are added to the reference assemblies starting in .NET Core 2.1.

例如,下面的 C# 代码在 .NET Core 2.0 中编译,但不在 .Net core 2.1 中编译:For example, the following C# code compiles in .NET Core 2.0, but not in .NET Core 2.1:

ConsoleKeyInfo key;    // Struct type

if (key.ToString() == "y")
{
    Console.WriteLine("Yes!");
}

在 .NET Core 2.1 中,之前的代码会导致以下编译器错误:CS0165 - 使用了未赋值的局部变量“key”In .NET Core 2.1, the previous code results in the following compiler error: CS0165 - Use of unassigned local variable 'key'

引入的版本Version introduced

2.12.1

使用 new 运算符或默认文本实例化结构类型。Instantiate struct types by using the new operator or default literal.

例如:For example:

ConsoleKeyInfo key = new ConsoleKeyInfo();    // Struct type.

if (key.ToString() == "y")
    Console.WriteLine("Yes!");
ConsoleKeyInfo key = default;    // Struct type.

if (key.ToString() == "y")
    Console.WriteLine("Yes!");

类别Category

Core .NET 库Core .NET libraries

受影响的 APIAffected APIs


macOS 上的 OpenSSL 版本OpenSSL versions on macOS

对于 AesCcmAesGcmDSAOpenSslECDiffieHellmanOpenSslECDsaOpenSslRSAOpenSslSafeEvpPKeyHandle 类型,macOS 上的 .NET Core 3.0 及更高版本的运行时现在首选 OpenSSL 1.1.x 版而非 OpenSSL 1.0.x 版。The .NET Core 3.0 and later runtimes on macOS now prefer OpenSSL 1.1.x versions to OpenSSL 1.0.x versions for the AesCcm, AesGcm, DSAOpenSsl, ECDiffieHellmanOpenSsl, ECDsaOpenSsl, RSAOpenSsl, and SafeEvpPKeyHandle types.

.NET Core 2.1 运行时现在支持 OpenSSL 1.1.x 版本,但仍首选 OpenSSL 1.0.x 版。The .NET Core 2.1 runtime now supports OpenSSL 1.1.x versions, but still prefers OpenSSL 1.0.x versions.

更改描述Change description

以前,.NET Core 运行时在 macOS 上使用 OpenSSL 1.0.x 版处理与 OpenSSL 交互的类型。Previously, the .NET Core runtime used OpenSSL 1.0.x versions on macOS for types that interact with OpenSSL. 最新的 OpenSSL 1.0.x 版 OpenSSL 1.0.2 现已不受支持。The most recent OpenSSL 1.0.x version, OpenSSL 1.0.2, is now out of support. 若要在支持的 OpenSSL 版本上保留使用 OpenSSL 的类型,.NET Core 3.0 及更高版本的运行时现需在 macOS 上使用较新版本的 OpenSSL。To keep types that use OpenSSL on supported versions of OpenSSL, the .NET Core 3.0 and later runtimes now use newer versions of OpenSSL on macOS.

通过此更改,macOS 上的 .NET Core 运行时的行为如下所示:With this change, the behavior for the .NET Core runtimes on macOS is as follows:

  • .NET Core 3.0 及更高版本运行时使用 OpenSSL 1.1.x(如果可用),并且仅在没有 1.1.x 版本可用的情况下才回退到 OpenSSL 1.0.x。The .NET Core 3.0 and later version runtimes use OpenSSL 1.1.x, if available, and fall back to OpenSSL 1.0.x only if there's no 1.1.x version available.

    对于将 OpenSSL 互操作类型与自定义 P/Invoke 一起使用的调用方,请按照 SafeEvpPKeyHandle.OpenSslVersion 注释中的指南进行操作。For callers that use the OpenSSL interop types with custom P/Invokes, follow the guidance in the SafeEvpPKeyHandle.OpenSslVersion remarks. 如果不检查 OpenSslVersion 值,你的应用可能会出现故障。Your app may crash if you don't check the OpenSslVersion value.

  • .NET Core 2.1 运行时使用 OpenSSL 1.0.x(如果可用),并且仅在没有 1.0.x 版本可用的情况下才回退到 OpenSSL 1.1.x。The .NET Core 2.1 runtime uses OpenSSL 1.0.x, if available, and falls back to OpenSSL 1.1.x if there's no 1.0.x version available.

    2.1 运行时首选早期版本的 OpenSSL,因为 .NET Core 2.1 中不存在 SafeEvpPKeyHandle.OpenSslVersion 属性,因此无法在运行时可靠地确定 OpenSSL 版本。The 2.1 runtime prefers the earlier version of OpenSSL because the SafeEvpPKeyHandle.OpenSslVersion property does not exist in .NET Core 2.1, so the OpenSSL version cannot be reliably determined at run time.

引入的版本Version introduced

  • .NET Core 2.1.16.NET Core 2.1.16
  • .NET Core 3.0.3.NET Core 3.0.3
  • .NET Core 3.1.2.NET Core 3.1.2

类别Category

Core .NET 库Core .NET libraries

受影响的 APIAffected APIs