zoukankan      html  css  js  c++  java
  • .NET Core 3.0及ASP.NET Core 3.0 前瞻

    前几天微软发布了 .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。

  • 相关阅读:
    有点忙啊
    什么是协程
    HDU 1110 Equipment Box (判断一个大矩形里面能不能放小矩形)
    HDU 1155 Bungee Jumping(物理题,动能公式,弹性势能公式,重力势能公式)
    HDU 1210 Eddy's 洗牌问题(找规律,数学)
    HDU1214 圆桌会议(找规律,数学)
    HDU1215 七夕节(模拟 数学)
    HDU 1216 Assistance Required(暴力打表)
    HDU 1220 Cube(数学,找规律)
    HDU 1221 Rectangle and Circle(判断圆和矩形是不是相交)
  • 原文地址:https://www.cnblogs.com/linezero/p/netcore3andaspnetcore3.html
Copyright © 2011-2022 走看看