.NET Core 2.2 正式釋出,有你喜歡的特性嗎?
微軟釋出了 .NET Core 2.2 版本,主要包含對執行時的診斷改進,對 ARM32 for Windows 和 Azure Active Directory for SQL Client 的支援。此版本最大的改進是在 ASP.NET Core 中。
ofollow,noindex">ASP.NET Core 2.2 和 Entity Framework Core 2.2 也已釋出。
你可以在 Windows、macOS 和 Linux 上下載並開始使用 .NET Core 2.2:
Visual Studio 15.9 、Visual Studio for Mac 和 Visual Studio Code 已支援 .NET Core 2.2 。
更新亮點:
分層編譯(Tiered Compilation)
分層編譯是一種使執行時能夠更自適應地使用實時(JIT)編譯器,以在啟動時獲得更好的效能並最大化吞吐量的功能。該功能在 .NET Core 2.1 中是可選的,然後在 .NET Core 2.2 Preview 2 中預設啟用。不過開發團隊認為還沒有準備好在正式的 .NET Core 2.2 版本中預設啟用它,所以已將其切換回可選功能。
分層編譯有望在 .NET Core 3.0 中預設啟用。
執行時事件(Runtime Events)
我們通常需要監視執行時服務(如當前程序的 GC,JIT 和 ThreadPool ),以瞭解這些服務在執行應用程式時的行為方式。在 Windows 系統上,這通常使用 ETW 監視當前程序的 ETW 事件來完成。雖然這種方法仍然有效,但使用 ETW 並不總是很容易。在一些低許可權環境中,或是在 Linux、macOS 上,都可能無法使用 ETW 。
從 .NET Core 2.2 開始,可以使用 EventListener 類來使用 CoreCLR 事件。這些事件描述了 GC,JIT,ThreadPool 和 interop 的行為。它們在 Windows 上作為 CoreCLR ETW 提供程式的一部分公開的相同事件。這允許應用使用這些事件或使用傳輸機制將它們傳送到遙測聚合服務。
訂閱事件示例程式碼:
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] != null ? eventData.Payload[i].ToString() : string.Empty; Console.WriteLine($"\tName = \"{eventData.PayloadNames[i]}\" Value = \"{payloadString}\""); } Console.WriteLine("\n"); } }
SqlConnection 支援 AccessToken
SQL Server 的 ADO.NET provider —— SqlClient,現在支援將 AccessToken 屬性設定為使用 Azure Active Directory 以對 SQL Server 連線進行身份驗證。要使用此功能,你可以使用 Microsoft.IdentityModel.Clients.ActiveDirectory NuGet 包中包含的 Active Directory Authentication Library for .NET 獲取 access token value 。
使用 Azure Active directory 驗證 SQL Server 連線示例:
// get access token using ADAL.NET var authContext = new AuthenticationContext(authority); var authResult = await authContext.AcquireTokenAsync(appUri, clientCredential); // setup connection to SQL Server var sqlConnection = new SqlConnection(connectionString); sqlConnection.AccessToken = authResult.AccessToken; await sqlConnection.OpenAsync();
此外,該版本還包含 Injecting code prior to Main,提供 Windows ARM32 支援等特性。
適用平臺:
-
Windows Client: 7, 8.1, 10 (1607+)
-
Windows Server: 2008 R2 SP1+
-
macOS: 10.12+
-
RHEL: 6+
-
Fedora: 26+
-
Ubuntu: 16.04+
-
Debian: 9+
-
SLES: 12+
-
openSUSE: 42.3+
-
Alpine: 3.7+
適用晶片:
-
x64 on Windows, macOS, and Linux
-
x86 on Windows
-
ARM32 on Linux (Ubuntu 16.04+, Debian 9+)
-
ARM32 on Windows (1809+; available in January)
完整詳細資訊請查閱.NET Core 2.2 發行說明 。