前幾天微軟釋出了 .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 @οnclick="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。
轉載于:https://www.cnblogs.com/linezero/p/netcore3andaspnetcore3.html