The best VPN 2024

The Best VPS 2024

The Best C# Book

.NET 6 Preview 2 released

Spread the love

.NET 6 Preview 2 released, The functional improvements of .NET 6 will be completed before July, after which the focus will be on quality improvements.

.NET 6 Preview 2 released Preface

On March 11, 2021, .NET 6 Preview 2 was released. This improvement mainly involves MAUI, new basic libraries and runtime, and JIT improvements.

The official version of .NET 6 will be released in November 2021, supporting systems such as Windows, macOS, Linux, Android and iOS, as well as x86, x86_64, ARM and ARM64 architectures. In addition, .NET 6 is the LTS version and will provide support for at least three years.

So let’s take a look at the contents. .NET 6 Preview 2 released

Topic: Improving internal loop performance

In the past few .NET versions, a lot of work has been done to improve throughput and reduce memory consumption. In .NET 6, improvements will be made to the internal loop performance: not only the pursuit of the best performance in applications and services, but also the best performance in application models, tool chains, and workflows.

Some of the work looks very similar to the traditional throughput optimization work in the past, but in fact, we do not focus on steady-state performance here, but focus on the startup performance of runtime, application model, command line, msbuild, etc., and end-to-end tools Performance (especially for smaller solutions).

The way of thinking involved in this optimization is usually very different from the way of thinking used when optimizing for steady-state throughput. For steady-state work, you may focus on caching values ​​that can be reused in the future, but for startup performance, you usually focus on operations that can only be called once, and the cost of the first call is important.

However, the work involved here is indeed the same as many other performance tasks. It has a typical measurement-analysis-repair cycle: analyze the relevant areas of the application to be optimized, analyze the result data to find the main causes and bottlenecks, and then They propose solutions and then restart the process of finding the next influential project.

We are still in the early .NET 6 development cycle, but we have successfully reduced the cost of key areas of developers involved in the inner loop, focusing on various dotnet command, for example newbuildand run. .NET 6 Preview 2 released

The improvements that have been included so far include:

Of course, one of the best performance optimizations is to avoid completing all the work. This is the focus of the other half of the .NET 6 theme: .NET hot reloading. By allowing the code to be edited while running the application or even without a debugger connected, hot reloading will increase developer productivity on all supported operating systems and hardware platforms. Developers do not need to recompile and start the program after modifying the code, the changes will take effect immediately, so that the entire change-build-run cycle can be skipped. This feature is expected to fundamentally improve the way .NET developers write applications and services.

.NET 6 Preview 2 released
.NET 6 Preview 2 released

The figure above shows the comparison of razor compilation time between .NET 5 and .NET 6 Preview 2. .NET 6 Preview 2 released

Subject: .NET has an excellent client development experience

One of the most exciting parts of .NET 6 is mobile development, which is currently available as a separate Xamarin product. Over time, we have been making Xamarin more similar to the mainline .NET. Now is the time to provide a fully unified mobile product for .NET. Using .NET 6, iOS, Android and macOS development will be integrated into the .NET SDK and use the .NET library. 

In the past two years, we have been working hard to integrate Mono into .NET, so developers can take advantage of the advantages of these two runtimes without having to target different .NET versions or worry about compatibility issues. In .NET 5, we moved Blazor WebAssembly and used the same model in Xamarin. .NET 6 is the culmination of this unification effort and covers a key part of the theme: Xamarin developers can upgrade to existing applications and use the latest .NET SDK.

Now that all your .NET applications will run on the same library, we hope to increase the amount of code shared on PC and mobile platforms. Xamarin.Forms, Xamarin’s cross-platform UI framework, is evolving into .NET MAUI, allowing you to easily write applications for iOS, Android, Windows and macOS using the same code base. .NET MAUI is provided as part of .NET 6, and a series of performance and tool improvements have also been made, such as .NET/C# hot reloading, sharing more resources and codes across different platforms, and a more flexible set of Better page rendering performance of UI controls.

.NET MAUI is not only for client application developers. Thanks to the refactored control set and functions that can run on the .NET 6 library, your existing Blazor applications can run natively on Windows and macOS through .NET MAUI. You will be able to seamlessly integrate native controls and features with the Blazor code base, including platform-specific features.

The last part of this topic is about packaging, deploying and publishing your cross-platform client application. Because there are too many developers/target platforms/methods for developing applications, you have to distribute many different application packages at the end of each day. Especially for the Blazor desktop, we want to make the experience as seamless as possible. We are working on strategies to improve distribution and version control locally and in the cloud.

To summarize, in .NET 6, you will be able to:

  • Build iOS, Android and macOS applications with .NET libraries
  • Use the same code to create iOS, Android, Windows and macOS client apps with .NET MAUI
  • Share code and resources between different platforms
  • Run Blazor apps natively on macOS and Windows
  • Easily package and distribute your programs

MAUI’s GitHub repository: http://github.com/dotnet/maui

MAUI update

The sample program of MAUI has been updated for .NET 6 Preview 2: https://github.com/dotnet/net6-mobile-samples , you can directly use the dotnet command line to build and start the application.

Mac Catalyst

Now you can add the following code to the project properties to build a macOS desktop application:

Copy<TargetFrameworks>net6.0-android;net6.0-ios</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">$(TargetFrameworks);net6.0-maccatalyst</TargetFrameworks>

.NET 6 Preview 2 released

Single multi-platform application project

The single project experience of .NET MAUI has been enabled, and you can adapt to Android, iOS and macOS at the same time through a project file; support for Windows will depend on WinUI 3, so this part will be added in the future. .NET 6 Preview 2 released

.NET 6 Preview 2 released
.NET 6 Preview 2 released

The figure above shows the development experience of multiple platforms in a single project.

Sharing fonts, pictures and application icons

Fonts and pictures can also be placed in the same place in your project, .NET MAUI will allow you to access them on all platforms, for example:

Copy<ItemGroup>
    <SharedImage Include="appicon.svg" ForegroundFile="appiconfg.svg" IsAppIcon="true" />
    <SharedFont Include="Resources\Fonts\ionicons.ttf" />
 </ItemGroup>

.NET 6 Preview 2 released

In addition to specifying specific files, it also supports using wild-card to match all files as shared pictures or fonts according to the path:

Copy<ItemGroup>
    <SharedImage Include="appicon.svg" ForegroundFile="appiconfg.svg" IsAppIcon="true" />
    <SharedImage Include="Resources\Images*" />
    <SharedFont Include="Resources\Fonts*" />
</ItemGroup>

.NET 6 Preview 2 released

The MAUI application uses HostBuilder to launch the program

Configure the MAUI program with an experience similar to ASP.NET Core configuration and support dependency injection. E.g:

Copypublic class Application : MauiApp
{
    public override IAppHostBuilder CreateBuilder() => 
        base.CreateBuilder()
            .RegisterCompatibilityRenderers()
            .ConfigureServices((ctx, services) =>
            {
                services.AddTransient<MainPage>();
                services.AddTransient<IWindow, MainWindow>();
            })
            .ConfigureFonts((hostingContext, fonts) =>
            {
                fonts.AddFont("ionicons.ttf", "IonIcons");
            });

    public override IWindow CreateWindow(IActivationState state)
    {
        Microsoft.Maui.Controls.Compatibility.Forms.Init(state);
        return Services.GetService<IWindow>();
    }
}

.NET 6 Preview 2 released

New control processor

.NET MAUI process introduces a new control mechanism, comprising a first set of the Preview 2 controls the use of these mechanisms: ButtonLabelEntrySliderand Switch. If you want to speed up the implementation of other controls, community PR is also welcome, see: https://github.com/dotnet/maui/wiki/Handler-Property-PR-Guidelines .

The sample program of .NET MAUI is now running on macOS, iOS and Android from the same project. The following is the running effect:

macOS :

.NET 6 Preview 2 released
.NET 6 Preview 2 released

iOS:

.NET 6 Preview 2 released
.NET 6 Preview 2 released

Android:

.NET 6 Preview 2 released
.NET 6 Preview 2 released

Mobile SDK update

Android

  • Set the default library to Android X

iOS

  • Developers on Windows can use the remote iOS simulator
  • Developers on Windows can connect to remote macOS to build applications
  • AOT has been added and enabled to support the deployment and distribution of iOS applications

.NET library update

There are also many updates to the .NET library in Preview 2.

System.Text.JsonIgnore circular references

System.Text.JsonNow supports ignore the circular reference, for the circular reference, it can no longer throw an exception, but like Newtonsoft.Jsonso simple to set up null:

Copyclass Node
{
    public string Description { get; set; }
    public object Next { get; set; }
}

void Test()
{
    var node = new Node { Description = "Node 1" };
    node.Next = node;

    var opts = new JsonSerializerOptions { ReferenceHandler = ReferenceHandler.IgnoreCycles };

    string json = JsonSerializer.Serialize(node, opts);
    Console.WriteLine(json); // Response {"Description":"Node 1","Next":null}
}

.NET 6 Preview 2 released

Priority queue PriorityQueue

.NET 6 Preview 2 added a new priority queue: System.Collections.Generic.PriorityQueue<TElement, TPriority>.

// Create a string queue with int as the priority
var pq = new PriorityQueue<string, int>();

// Various elements join the team
pq.Enqueue("A", 3);
pq.Enqueue("B", 1);
pq.Enqueue("C", 2);
pq.Enqueue("D", 3);

pq.Dequeue(); // returns "B"
pq.Dequeue(); // returns "C"
pq.Dequeue(); // returns "A" or "D"

.NET 6 Preview 2 released

Improved numerical format analysis

For standard numeric format, we have improved its parser, especially for .ToStringand .TryFormatimproved. The result when the precision is greater than 99 digits after the decimal point has now been improved, and better support for trailing 0s is also provided:

  • 32.ToString("C100"):
    • .NET 6:32.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    • .NET 5: There is a limit of 99 digits of precision
  • 32.ToString("H99")And 32.ToString("H100"):
    • .NET 6: Throw FormatException
    • .NET 5: It His an invalid format modification, but instead of throwing an exception, it returns an error result
  • double.Parse("9007199254740997.0"):
    • .NET 6:9007199254740996
    • .NET 5:9007199254740998

SignalR’s nullable type annotation

SingleR has now completed the labeling of the nullable type.

Runtime update

.NET 6 Preview 2 also has many improvements in runtime. .NET 6 Preview 2 released

The framework assembly uses Crossgen2 precompilation

All .NET libraries are now pre-compiled using crossgen 2 and are currently limited to the basic .NET libraries. For other libraries such as ASP.NET Core and Windows Desktop, they will be gradually migrated to crossgen 2 in subsequent preview versions.

Crossgen 2 itself is not focused on performance improvement, but is used to enable new performance features (such as PGO). However, crossgen 2 brings some improvements in hard disk space usage:

CopySize [MB] FullName
--------- --------
64.22     C:Program FilesdotnetsharedMicrosoft.NETCore.App5.0.3
63.31     C:Program FilesdotnetsharedMicrosoft.NETCore.App6.0.0-preview.1.21102.12
63.00     C:Program FilesdotnetsharedMicrosoft.NETCore.App6.0.0-preview.2.21118.6

PGO

.NET 6 Preview 2 adds the following improvements:

JIT improvements

.NET 6 Preview 2 includes the following improvements for JIT:

The analysis of the results of these improvements can be viewed here:

In addition, the optimization of ARM64 is constantly being carried out with ARM engineers.

Conclusion

The above are the improvements in .NET 6 Preview 2.

The functional improvements of .NET 6 will be completed before July, after which the focus will be on quality improvements.

Leave a Comment