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

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

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

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

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

博客園也在前些天升級爲.NET Core 3.0 Preview 8,目前運行算是良好。json

下面實際體驗.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中。app

最新版本安裝

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。

 
分類:  .NET CoreASP.NET Core
好文要頂  關注我  收藏該文   
6
0
 
 
 
相關文章
相關標籤/搜索