.NET 8
.NET 8, the latest version of Microsoft's .NET framework, boasts impressive performance, developer productivity, and an array of tools for building cloud-native apps, web, desktop, and mobile applications. The .NET 8 release date is set to coincide with the .NET Conf 2023 event in November.
KEY TAKEAWAYS:
- .NET 8 will be LTS release which means it will be supported for three years.
- .NET 8 includes a number of new features and improvements including support for native AOT compilation, server-side rendering for Blazor, and simplified output paths.
- .NET 8 is still in Preview, so it is not recommended for use in production applications.
- Developers should explore the .NET 8 roadmap and test its new features to get ready for the LTS release in November 2023.
Currently, .NET 8 Preview 3 is available for download and includes a range of improvements, such as changes to build paths, workloads, Microsoft.Extensions, and containers, as well as performance enhancements in JIT, Arm64, and dynamic PGO.
As a long-term support (LTS) release, .NET 8 will be supported for three years. Note that this information relates to a pre-release product that may undergo significant changes before its official release. It's noteworthy that a significant portion of the existing .NET documentation on Microsoft's website has yet to be updated for .NET 8.
What's New in .NET 8
The SDK underwent multiple enhancements and modifications, including breaking changes. In .NET 8, there are several new enhancements and changes that you should be aware of. These include improvements to the dotnet workload clean command, as well as simplified output paths.
In addition, there are updates to the Runtime and Native code generation capabilities, as well as changes to how Containers are handled. Finally, there is a new Environment Variable available for non-root user UID values.
.NET 8 Features
-
ASP.NET Core 8 Updates in .NET 8:
ASP.NET Core in .NET 8 Preview 3 boasts several notable enhancements, including:
- Native AOT support
- Server-side rendering for Blazor
- Ability to render Razor components independently of ASP.NET Core
- Blazor Sections support
- Monitoring of Blazor Server circuit activity
- Blazor WebAssembly apps enabled with SIMD by default
- Request timeouts
- Short circuit routes
-
Output paths have been simplified:
.NET 8 Preview 3 has a new feature that simplifies the output path and folder structure for build outputs. Before this update, .NET applications created multiple deep and intricate output paths for various build artifacts. The updated, simplified output path structure aggregates all build outputs into a single location, making it easier for tools to predict.
To take advantage of the new output path format, you need to set the UseArtifactsOutput property to true in your Directory.Build.props file.
-
Command to clean up workload packs:
Workload packs may be left behind after updating .NET SDK or Visual Studio, causing confusion for users. Some users resort to deleting directories manually, which is not recommended. Instead, .NET 8 Preview 3 introduces a new command to clean up leftover workload packs. The command is 'dotnet workload clean'. Use it to safely restore to a known-good state before trying again. The command has two modes of operation, which are discussed next.
‘dotnet workload clean’: The 'dotnet workload clean' command performs a garbage collection process for file-based or MSI-based workloads. In this mode, the garbage collection only removes orphaned packs without affecting anything else. Orphaned packs refer to packs from uninstalled versions of the .NET SDK or packs without installation records.
It only removes orphaned packs from the given SDK version or older. If a newer version of SDK is installed, the command needs to be repeated. If Visual Studio is installed and also manages workloads, 'dotnet workload clean' will display a list of all Visual Studio Workloads installed on the machine. These workloads must be uninstalled via Visual Studio rather than using the .NET SDK CLI. This helps to avoid confusion about why some workloads are not removed after running the 'dotnet workload clean' command.
‘dotnet workload clean --all’: Compared to workload clean , workload clean --all is more aggressive as it cleans up every pack on the machine that is not from Visual Studio and is of the current SDK workload installation type. This command also removes all workload installation records for the running .NET SDK feature band and below. However, unlike "workload clean," it does not remove installation records since the manifests are the only way to map a pack to the workload ID, but they may not exist for orphaned packs.
-
System.Text.Json Serialization:
System.Text.Json has undergone several improvements, particularly in terms of performance and reliability when used with ASP.NET Core in Native AOT apps. These enhancements include the support for serializing types with required and init properties, which were only previously supported in reflection-based serialization. Additionally, it is now possible to customize the handling of members that are not present in the JSON payload and serialize properties from interface hierarchies. Below is an example of how this works in code.
IDerived value = new DerivedImplement { Base = 0, Derived =1 }; JsonSerializer.Serialize(value); // {"Base":0,"Derived":1} public interface IBase { public int Base { get; set; } } public interface IDerived : IBase { public int Derived { get; set; } } public class DerivedImplement : IDerived { public int Base { get; set; } public int Derived { get; set; } }
New naming policies have been added to the JsonNamingPolicy for converting property names to snake_case (with an underscore) and kebab-case (with a hyphen). These policies can be used in a similar manner to the existing JsonNamingPolicy.CamelCase policy. Additionally, the JsonSerializerOptions now has a MakeReadOnly() method that provides explicit control over when a JsonSerializerOptions instance is frozen, which can also be checked using the IsReadOnly property.
What's Next in .NET 8?
If you're interested to expand your knowledge about .NET 8, you may explore the official documentation or resources for .NET 8 Preview 3 for the following:
- Methods for working with randomness like GetItems<T>() and Shuffle<T>()
- Performance-focused new types
- System.Numerics and System.Runtime.Intrinsics namespace improvements
- System.ComponentModel.DataAnnotations namespace new Data validation attributes
- New extension libraries
- Reflection improvements
- Native AOT support
- These features and improvements offer many exciting possibilities for developers, and exploring them further may help you to take full advantage of .NET 8 Preview 3.
Should I use .NET 7 or 8?
As .NET 8 is still in the Preview version, we will strongly recommend you keep your critical and production applications in previous versions until we have .NET 8 LTS version in hand, likely by November 2023.
But you should definitely explore .NET 8 roadmap and test its new exciting features to get ready yourself.