.NET Core 2.2 的新增功能What's new in .NET Core 2.2

.NET Core 2.2 包括在应用程序部署、运行时服务的事件处理、Azure SQL 数据库的身份验证、JIT 编译器性能,以及执行 Main 方法之前的代码注入方面的增强功能。.NET Core 2.2 includes enhancements in application deployment, event handling for runtime services, authentication to Azure SQL databases, JIT compiler performance, and code injection prior to the execution of the Main method.

新部署模式New deployment mode

从 .NET Core 2.2 开始,可以部署依赖于框架的可执行文件,这是“.exe”文件而不是“.dll”文件。Starting with .NET Core 2.2, you can deploy framework-dependent executables, which are .exe files instead of .dll files. 与依赖框架的部署在功能上类似,依赖框架的可执行文件 (FDE) 仍然依赖于存在的 .NET Core 的共享系统级版本来运行。Functionally similar to framework-dependent deployments, framework-dependent executables (FDE) still rely on the presence of a shared system-wide version of .NET Core to run. 应用程序只包含代码和任何第三方依赖项。Your app contains only your code and any third-party dependencies. 与依赖框架的部署不同,FDE 特定于平台。Unlike framework-dependent deployments, FDEs are platform-specific.

这种新的部署模式在构建可执行文件(而不是库)方面具有独特优势,这意味着你可以直接运行应用程序,而无需首先调用 dotnetThis new deployment mode has the distinct advantage of building an executable instead of a library, which means you can run your app directly without invoking dotnet first.

核心Core

在运行时服务中处理事件Handling events in runtime services

你可能经常希望监视应用程序的运行时服务(如 GC、JIT 和 ThreadPool)的使用情况,以了解它们如何影响应用程序。You may often want to monitor your application's use of runtime services, such as the GC, JIT, and ThreadPool, to understand how they impact your application. 在 Windows 系统上,这通常通过监视当前进程的 ETW 事件来完成。 On Windows systems, this is commonly done by monitoring the ETW events of the current process. 虽然这仍然可以很好地工作,但是如果你在低特权环境中或者在 Linux 或 macOS 上运行,那么并不总是能够使用 ETW。 While this continues to work well, it's not always possible to use ETW if you're running in a low-privilege environment or on Linux or macOS. 

从 .NET Core 2.2 开始,现在可以使用 System.Diagnostics.Tracing.EventListener 类来使用 CoreCLR 事件。Starting with .NET Core 2.2, CoreCLR events can now be consumed using the System.Diagnostics.Tracing.EventListener class. 这些事件描述了诸如 GC、JIT、ThreadPool 和 interop 等运行时服务的行为。These events describe the behavior of such runtime services as GC, JIT, ThreadPool, and interop. 这些事件与作为 CoreCLR ETW 提供程序的一部分公开的事件相同。These are the same events that are exposed as part of the CoreCLR ETW provider.  这允许应用程序使用这些事件或使用传输机制将它们发送到遥测聚合服务。  This allows for applications to consume these events or use a transport mechanism to send them to a telemetry aggregation service. 可以在以下代码示例中看到如何订阅事件:You can see how to subscribe to events in the following code sample:

internal sealed class SimpleEventListener : EventListener
{
    // Called whenever an EventSource is created.
    protected override void OnEventSourceCreated(EventSource eventSource)
    {
        // Watch for the .NET runtime EventSource and enable all of its events.
        if (eventSource.Name.Equals("Microsoft-Windows-DotNETRuntime"))
        {
            EnableEvents(eventSource, EventLevel.Verbose, (EventKeywords)(-1));
        }
    }

    // Called whenever an event is written.
    protected override void OnEventWritten(EventWrittenEventArgs eventData)
    {
        // Write the contents of the event to the console.
        Console.WriteLine($"ThreadID = {eventData.OSThreadId} ID = {eventData.EventId} Name = {eventData.EventName}");
        for (int i = 0; i < eventData.Payload.Count; i++)
        {
            string payloadString = eventData.Payload[i]?.ToString() ?? string.Empty;
            Console.WriteLine($"\tName = \"{eventData.PayloadNames[i]}\" Value = \"{payloadString}\"");
        }
        Console.WriteLine("\n");
    }
}

此外,.NET Core 2.2 向 EventWrittenEventArgs 类添加了以下两个属性来提供有关 ETW 事件的其他信息:In addition, .NET Core 2.2 adds the following two properties to the EventWrittenEventArgs class to provide additional information about ETW events:

数据Data

使用 SqlConnection.AccessToken 属性对 Azure SQL 数据库进行 AAD 身份验证AAD authentication to Azure SQL databases with the SqlConnection.AccessToken property

从 .NET Core 2.2 开始,由 Azure Active Directory 颁发的访问令牌可用于对 Azure SQL 数据库进行身份验证。Starting with .NET Core 2.2, an access token issued by Azure Active Directory can be used to authenticate to an Azure SQL database. 若要支持访问令牌,必须将 AccessToken 属性添加到 SqlConnection 类。To support access tokens, the AccessToken property has been added to the SqlConnection class. 若要利用 AAD 身份验证,请下载 System.Data.SqlClient NuGet 包的版本 4.6。To take advantage of AAD authentication, download version 4.6 of the System.Data.SqlClient NuGet package. 若要使用功能,可以使用包含在 Microsoft.IdentityModel.Clients.ActiveDirectory NuGet 包中的适用于 .NET 的 Active Directory 身份验证库获取访问令牌值。In order to use the feature, you can obtain the access token value using the Active Directory Authentication Library for .NET contained in the Microsoft.IdentityModel.Clients.ActiveDirectory NuGet package.

JIT 编译器改进JIT compiler improvements

分层编译仍然是一项可选功能Tiered compilation remains an opt-in feature

在 .NET Core 2.1,JIT 编译器实现了一项新的编译器技术,即“分层编译”,作为可选功能。In .NET Core 2.1, the JIT compiler implemented a new compiler technology, tiered compilation, as an opt-in feature. 分层编译旨在提高性能。The goal of tiered compilation is improved performance. 由 JIT 编译器执行的重要任务之一是优化代码执行。One of the important tasks performed by the JIT compiler is optimizing code execution. 然而,对于很少使用的代码路径,相比执行未优化代码所花费的运行时,编译器可能需要更多的时间来优化代码。For little-used code paths, however, the compiler may spend more time optimizing code than the runtime spends executing unoptimized code. 分层编译介绍了 JIT 编译中的两个阶段:Tiered compilation introduces two stages in JIT compilation:

  • 第一层,将尽可能快地生成代码。A first tier, which generates code as quickly as possible.

  • 第二层,将为那些频繁执行的方法生成优化代码。A second tier, which generates optimized code for those methods that are executed frequently. 为了增强性能,第二层编译并行执行。The second tier of compilation is performed in parallel for enhanced performance.

有关分层编译可能带来的性能改进的信息,请参阅宣布发布 .NET Core 2.2 预览版 2For information on the performance improvement that can result from tiered compilation, see Announcing .NET Core 2.2 Preview 2.

在 .NET Core 2.2 预览版 2 中,默认情况下已启用分层编译。In .NET Core 2.2 Preview 2, tiered compilation was enabled by default. 但是,我们仍然没有准备好在默认情况下启用分层编译。However, we've decided that we are still not ready to enable tiered compilation by default. 因此在 .NET Core 2.2 中,分层编译仍是一项可选功能。So in .NET Core 2.2, tiered compilation continues to be an opt-in feature. 有关选择加入分层编译的信息,请参阅 .NET Core 2.1 中的新增功能中的 Jit 编译器改进For information on opting in to tiered compilation, see Jit compiler improvements in What's new in .NET Core 2.1.

运行时Runtime

在执行 Main 方法之前注入代码Injecting code prior to executing the Main method

从 .NET Core 2.2 开始,可以使用启动挂钩注入代码,然后再运行应用程序的 Main 方法。Starting with .NET Core 2.2, you can use a startup hook to inject code prior to running an application's Main method. 启动挂钩使主机可以在部署应用程序之后自定义其行为,而不需要重新编译或更改应用程序。Startup hooks make it possible for a host to customize the behavior of applications after they have been deployed without needing to recompile or change the application.

我们希望托管提供商定义自定义配置和策略,包括可能会影响主入口点加载行为的设置,如 System.Runtime.Loader.AssemblyLoadContext 行为。We expect hosting providers to define custom configuration and policy, including settings that potentially influence the load behavior of the main entry point, such as the System.Runtime.Loader.AssemblyLoadContext behavior. 挂钩可用于设置跟踪或遥测注入,以设置回调进行处理,或定义其他环境相关的行为。The hook can be used to set up tracing or telemetry injection, to set up callbacks for handling, or to define other environment-dependent behavior. 挂钩独立于入口点,因此不需要修改用户代码。The hook is separate from the entry point, so that user code doesn't need to be modified.

有关详细信息,请参阅主机启动挂钩See Host startup hook for more information.

请参阅See also