.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.</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)编译的形式。
示例提升:
要启用ReadyToRun编译 需要以下操作:
将PublishReadyToRun属性设置为true。 使用显式发布RuntimeIdentifier。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.</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(, ) })
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。
最新文章
- iOS 关于PCH文件(全局文件)的介绍
- vs2015 HTTP Error 400. The request hostname is invalid.
- day9-协程
- android: PendingIntent的使用
- Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree andsum =
- Linux运维命令之一
- web医疗影像浏览demo及地址
- WPF-控件-层级控件-TreeView
- bzoj 1089 [SCOI2003]严格n元树(DP+高精度)
- HTTP协议(二)
- git之commit
- 解题(GoUpstairs -- 上楼梯)
- cf1136E. Nastya Hasn&#39;t Written a Legend(二分 线段树)
- JDBC创建链接的几种方式
- 《Linux内核分析》期终总结
- cad 关键字被保留了?选择集关键字保留了? N S W E关键字无法用?
- osgearth2.8关于RectangleNodeEditor编辑点不可见的问题
- Docker Toolbox更换镜像源并下载centos7
- 【BZOJ4455】小星星(动态规划,容斥)
- android 同一个service启动之后 能不能被绑定bind
热门文章
- Socket编程:UDP和TCP概论及案例
- [Inno Setup]写入注册表时32位系统和64位系统的路由
- 关于JVM内存溢出的原因分析及解决方案探讨
- 基于jmeter+perfmon的稳定性测试记录
- Linux之各程序安装
- ABAP-根据采购订单行项目统计供应商未清额和已清额
- pycharm的补充
- 使用Cmake编译CEF时遇到Error in configuration process,project file may be invalid的解决办法
- 建立apk定时自动打包系统第三篇——代码自动更新、APP自动打包系统
- (四十二)c#Winform自定义控件-进度条扩展