.NET Core 3.0及ASP.NET Core 3.0 前瞻

  • 2019 年 10 月 3 日
  • 筆記

前幾天微軟發布了 .NET Core 3.0 Preview 9 ,這是.NET Core 3.0 最後一個預覽版。

.NET Core 3.0 正式發布將在.NET Conf 上發布,.NET Conf 時間是9月23日至25日。

Visual Studio 2019 16.3預覽版3和Visual Studio for Mac 8.3支援.NET Core 3.0 ,這些版本也同時發布。

從.NET Core 3.0 Preview 7就可用於生產,目前dotnet官網就是使用 https://dotnet.microsoft.com/ Powered by .NET Core 3.0.0-preview9-19423-09。

部落格園也在前些天升級為.NET Core 3.0 Preview 8,目前運行算是良好。

下面實際體驗.NET Core 3.0 新特性。

.NET Core 3.0

System.Text.Json

示例:

    public class Person      {          public string FirstName { get; set; }          public string LastName { get; set; }          public DateTime? BirthDay { get; set; }      }      //轉成對象      string json = ...      Person person = JsonSerializer.Parse<Person>(json);        //轉成json字元串      Person person = ...      string json = JsonSerializer.ToString(person);

.NET Standard 2.1

要以.NET Standard 2.1為目標,必須編輯項目文件並將TargetFramework屬性更改為netstandard2.1: .NET Framework不支援.NET Standard 2.1。

<Project Sdk="Microsoft.NET.Sdk">      <PropertyGroup>      <TargetFramework>netstandard2.1</TargetFramework>    </PropertyGroup>    </Project>

 

Microsoft.Data.SqlClient

Microsoft.Data.SqlClient是Microsoft Sql Server的數據提供程式。

它是兩個System.Data.SqlClient組件的聯合體,獨立存在於.NET Framework和.NET Core中。

最新版本安裝

Install-Package Microsoft.Data.SqlClient 

https://github.com/dotnet/SqlClient 

發布成單個程式

dotnet publish -r win10-x64 /p:PublishSingleFile=true

Alpine Docker images

.NET Core and ASP.NET Core on ARM64

docker pull mcr.microsoft.com/dotnet/core/runtime:3.0-alpine-arm64v8 

docker pull mcr.microsoft.com/dotnet/core/aspnet:3.0-alpine-arm64v8

dotnet-counters

安裝 : dotnet tool install --global dotnet-counters --version 3.0.0-preview8.19412.1

使用示例:

顯示所有資訊

dotnet-counters monitor --process-id 1902 System.Runtime

顯示CPU使用 GC 及異常數

dotnet-counters monitor --process-id 1902 System.Runtime[cpu-usage,gc-heap-size,exception-count]

官方文檔:https://github.com/dotnet/diagnostics/blob/master/documentation/dotnet-counters-instructions.md

ReadyToRun

你可以通過將應用程式集編譯為ReadyToRun(R2R)格式來縮短.NET Core應用程式的啟動時間。R2R是一種提前(AOT)編譯的形式。

示例提升:

僅限IL的應用:

啟動時間:1.9秒
記憶體使用量:69.1 MB
應用程式大小:150 MB
使用ReadyToRun影像:

啟動時間:1.3秒。
記憶體使用量:55.7 MB
應用程式大小:156 MB

要啟用ReadyToRun編譯 需要以下操作:

將PublishReadyToRun屬性設置為true。 使用顯式發布RuntimeIdentifier。

<Project Sdk="Microsoft.NET.Sdk">    <PropertyGroup>      <OutputType>Exe</OutputType>      <TargetFramework>netcoreapp3.0</TargetFramework>      <PublishReadyToRun>true</PublishReadyToRun>    </PropertyGroup>  </Project>

 

dotnet publish -r win-x64 -c Release

ReadyToRun編譯器目前不支援交叉定位。需要在給定目標上進行編譯。例如,如果想要Windows x64的R2R程式,則需要在該環境中運行publish命令。

IL linker

使用IL linker 可以將程式大小從大約68MB減少到大約28MB

dotnet publish -r win10-x64 -c Release /p:PublishTrimmed=true /p:PublishSingleFile=true

HttpClient支援HTTP/2

使用示例:

var client = new HttpClient() { BaseAddress = new Uri("https://localhost:5001") };  // HTTP/1.1 request  using (var response = await client.GetAsync("/"))  {      Console.WriteLine(response.Content);  }  // HTTP/2 request  using (var request = new HttpRequestMessage(HttpMethod.Get, "/") { Version = new Version(2, 0) })  using (var response = await client.SendAsync(request))  {      Console.WriteLine(response.Content);  }

 

ASP.NET Core 3.0

前一篇也有介紹ASP.NET Core 3.0預覽版體驗。

ASP.NET Core 3.0中主要更新還是Blazor和gRPC

Blazor

Blazor 是一個用於使用 .NET 生成互動式客戶端 Web UI 的框架:

  • 使用 C# 代替 JavaScript 來創建豐富的互動式 UI。
  • 共享使用 .NET 編寫的伺服器端和客戶端應用邏輯。
  • 將 UI 呈現為 HTML 和 CSS,以支援眾多瀏覽器,其中包括移動瀏覽器。

使用 .NET 進行客戶端 Web 開發可提供以下優勢:

  • 使用 C# 代替 JavaScript 來編寫程式碼。
  • 利用現有的 .NET 庫生態系統。
  • 在伺服器和客戶端之間共享應用邏輯。
  • 受益於 .NET 的性能、可靠性和安全性。
  • 始終高效支援 Windows、Linux 和 macOS 上的 Visual Studio。
  • 以一組穩定、功能豐富且易用的通用語言、框架和工具為基礎來進行生成。

Blazor 應用基於組件 。 Blazor 中的組件是指 UI 元素,例如,頁面、對話框或數據輸入窗體。

組件類通常以 Razor 標記頁(文件擴展名為 .razor )的形式編寫。 Blazor 中的組件有時被稱為 Razor 組件 。

Razor 標記演示組件:

<div>      <h1>@Title</h1>        @ChildContent        <button @onclick="OnYes">Yes!</button>  </div>    @code {      [Parameter]      public string Title { get; set; }        [Parameter]      public RenderFragment ChildContent { get; set; }        private void OnYes()      {          Console.WriteLine("Write to the console in C#! 'Yes' button was selected.From LineZero");      }

對話框的正文內容 (ChildContent) 和標題 (Title) 由在其 UI 中使用此組件的組件提供。 OnYes 是由按鈕的 onclick 事件觸發的 C# 方法。

Blazor 使用 UI 構成的自然 HTML 標記。 HTML 元素指定組件,並且標記的特性將值傳遞給組件的屬性。

在以下示例中,Index 組件中使用上面的 Dialog 組件。

@page "/"    <h1>Hello, world!</h1>    Welcome to your new app.    <Dialog Title="Blazor">      Do you want to <i>learn more</i> about Blazor?
   From LineZero
</Dialog>

更多官方介紹:https://docs.microsoft.com/zh-cn/aspnet/core/blazor/get-started?view=aspnetcore-3.0&tabs=visual-studio

gRPC

gRPC 的主要優點是:

  • 現代高性能輕量級 RPC 框架。
  • 協定優先 API 開發,默認使用協議緩衝區,允許與語言無關的實現。
  • 可用於多種語言的工具,以生成強類型伺服器和客戶端。
  • 支援客戶端、伺服器和雙向流式處理調用。
  • 使用 Protobuf 二進位序列化減少對網路的使用。

這些優點使 gRPC 適用於:

  • 效率至關重要的輕量級微服務。
  • 需要多種語言用於開發的 Polyglot 系統。
  • 需要處理流式處理請求或響應的點對點實時服務。

雖然 C# 實現目前在官方 gRPC 上有介紹,但當前實現依賴於用 C (gRPC C-core) 編寫的本機庫。 

目前正在基於 Kestrel HTTP 伺服器和完全託管的 ASP.NET Core 實現gRPC。

Tags: