1.前言
.Net8 RC1经历多个预览版的迭代,于2023年9月12日发布了。按照官方的介绍,它是两个候选版本中的一个。这其实也预示着离真正的.Net8 LTS版不远了?本篇看下部分功能
2.概述
一:System.Text.Json改进
System.Net.Http.JsonIAsyncEnumerable的扩展
RC1包含 IAsyncEnumerable 流式反序列化扩展方法
const string RequestUri = "https://api.contoso.com/books";
using var client = new HttpClient();
IAsyncEnumerable<Book> books = await client.GetFromJsonAsAsyncEnumerable<Book>(RequestUri);
await foreach (Book book in books)
{
Console.WriteLine($"Read book '{book.title}'");
}
public record Book(int id, string title, string author, int publishedYear);
二:AndroidStripILAfterAOT安卓模式
一般来说程序的目的主要是兼容预编译AOT的体积大小和速度快慢,如果你不是关系程序大小而是性能,那么你就可以使用它。
为 Android 应用设置以下 MsBuild 属性:
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<AndroidStripILAfterAOT>true</AndroidStripILAfterAOT>
</PropertyGroup>
默认情况下设置为true将覆盖默认设置,从而允许删除所有可修剪的AOT方法。通过显式设置以下两个选项,可以同时使用分析的AOT和IL剥离:
AndroidStripILAfterAOTAndroidEnableProfiledAot
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<AndroidStripILAfterAOT>true</AndroidStripILAfterAOT>
<AndroidEnableProfiledAot>true</AndroidEnableProfiledAot>
</PropertyGroup>
三:WasmStripILAfterAOTWASM模式
Blazor WebAssembly和WASM浏览器支持提前AOT编译,你可以在其中将 .NET 代码直接编译到WebAssembly中。AOT编译可提高运行时性能,但代价是应用大小更大。这种新的剥离模式根据我们在上述 github 问题中提到的测试,将_framework文件夹的大小减少了 1.7% – 4.2%。
何时考虑使用 :WasmStripILAfterAOT
此功能非常适合启用 AOT 编译的任何时候,因此请尝试较小的应用程序!
如何使用:WasmStripILAfterAOT
为 Blazor WebAssembly 或WASM浏览器应用设置以下MsBuild属性:
<PropertyGroup>
<RunAOTCompilation>true</RunAOTCompilation>
<WasmStripILAfterAOT>true</WasmStripILAfterAOT>
</PropertyGroup>
它将修剪大多数已编译方法的IL代码,包括库中的方法和应用开发人员编写的方法。
更多可以了解微软官方链接:
https://devblogs.microsoft.com/dotnet/announcing-dotnet-8-rc1/