In this article, we’re going to delve into the latest updates and enhancements introduced in .NET 8 Preview 4. Microsoft has been hard at work, refining and expanding the capabilities of the .NET ecosystem, and this preview release is no exception.
We’ll be exploring the key features, such as improvements to System.Text.Json, code generation optimizations, and support for unspeakable types.
Let’s check what’s new in .NET 8 Preview 4!
A Fresh Take on MSBuild Output
Ever struggled with MSBuild output? Parsing that wall of text can be a nightmare, right? Well, Microsoft heard you and now we’ve got the Terminal Logger! This modernized MSBuild output logging is here to make your life easier. It’s designed to:
- Organize errors logically by project
- Display projects and builds in a user-friendly way (especially for multi-framework projects)
- Differentiate various
TargetFrameworks
of a project - Provide a complete overview of the project’s outputs
- Keep you updated on the build progress
Source: Microsoft
To enable the new output, use the /tl
flag along with options like auto
, on
, or off
. With this logger turned on, you’ll see different phases (e.g., restoration and construction) and built projects with MSBuild targets and timings. This extra info helps you understand the build process and where to start exploring further.
As projects get built, a new “build completed” section will appear, showing:
- Project name
- Target Framework
- Main compilation output
- Compilation diagnostics
Source: Microsoft
Now, both the project and any errors stand out, making it easier to spot issues.
SDK: Tweaked Simplified Output Path
Remember the Preview 3 simplified output path? Microsoft took your feedback onboard, and now we have some updates:
- Default path changed from
.artifacts
toartifacts
- Project file usage no longer supported
- Easier activation with
buildprops
template fordotnet new
Why these changes? Most users wanted better visibility on Unix-based systems, so the .
was dropped from the folder name. Also, using artifacts
as the root path resolved .gitignore support and .NET SDK file globbing issues.
To try this updated feature, simply run dotnet new buildprops --use-artifacts
and you’re good to go!
Template Engine: Boosted Security with NuGet.org Packages
There is now a new security measure in the dotnet template engine. What’s new? Here’s what’s new:
- Blocking
http:\\
feed package downloads (override with--force
)
Source: Microsoft
- Warning users about template package vulnerabilities
Source: Microsoft
- Adding info on NuGet.org prefix reserved templates
Source: Microsoft
- Template package owner information
Source: Microsoft
NuGet: Package Signing Verification on Linux, Anyone?
Hey C# devs, guess what? Now, NuGet will start to verify signed packages on Linux by default! Yeah, that’s right! It’s been happening on Windows already, but macOS is still missing out.
For most of you Linux folks, no biggie, right? But hold on, if you’ve got a root certificate bundle chilling at /etc/pki/ca-trust/extracted/pem/objsign-ca-bundle.pem
, you might run into some trust issues and bump into NU3042. Oops! Wanna skip the verification part? No problemo! Just set the DOTNET_NUGET_SIGNATURE_VERIFICATION
environment variable to false
. Easy peasy!
NuGet: Checking Package Dependencies for Security Flaws
Enable NuGet security auditing, and dotnet restore
will generate a report of security vulnerabilities, including the impact, severity, and affected package versions. To activate this feature, add <EnablePackageVulnerabilityAnalysis>true</EnablePackageVulnerabilityAnalysis>
to your project file or Directory.Build.props
.
Supercharge Your Security with Auditing
Ready to boost your project’s security? Now you can opt for security audit reports! You can achieve this by adding the next MSBuild property to your .csproj
or MSBuild file:
<NuGetAudit>true</NuGetAudit>
And bam! You’re in. And hey, don’t forget to define NuGet.org as one of your package sources to access the juicy vulnerability dataset:
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
Handle Vulnerable Packages like a Pro
Adding a package with known vulnerabilities? No worries, dotnet restore
has your back with a heads-up warning! Run dotnet restore
, and you’ll get warnings for each affected package and advisory. Neat, huh?
Oh, and check out these warning codes for different severity levels:
Source: Microsoft
Want more control? Set the <NuGetAuditLevel>
MSBuild property to your desired level. Choose from low
, moderate
, high
, and critical
. So, if you’re into moderate
, high
, and critical
advisories, check this example:
<NuGetAuditLevel>moderate</NuGetAuditLevel>
Embrace the Power of UTF8
Now, the new IUtf8SpanFormattable
interface, which is like its buddy ISpanFormattable
, but for UTF8 and Span<byte>
. Sound cool? It gets better. It’s now implemented on all primitive types using shared logic, so you can format directly to UTF8 from various types like Byte
, DateTime
, and even Rune
.
Plus, the new Utf8.TryWrite
methods are the UTF8 versions of MemoryExtensions.TryWrite
. You can use interpolated string syntax to format complex expressions directly into a span of UTF8 bytes. Sweet!
Time Traveling Testers, Rejoice!
Introducing the TimeProvider
abstract class! Mock time like a boss in your test scenarios, even mock Task operations too. Grab local and UTC time, measure performance with timestamps, and create timers effortlessly.And the icing on the cake? There’s a netstandard 2.0 library called Microsoft.Bcl.TimeProvider
for supported .NET Framework versions and earlier .NET editions.
Unleash the Power of Vector512 and AVX-512
.NET 8 is all about that SIMD life with System.Runtime.Intrinsics.Vector512<T>
and AVX-512 for x86/x64 hardware. Preview 4 serves up 512-bit vector operations, 16 bonus SIMD registers, and extra instructions for 128-bit, 256-bit, and 512-bit vectors. Curious if your hardware’s up for it? Just check Vector512.IsHardwareAccelerated
!
AOT All the Way!
Did you know? .NET 8 Preview 4’s default console
template is AOT-ready! Run dotnet new console --aot
and voilà, an AOT-compiled project is yours. Choose an optimization preference like Speed or Size to hit that sweet spot.
Keep Your Linux Game Strong
.NET 8 Preview 4 updates the supported Linux distro versions and has its sights set on Ubuntu 16.04 for all architectures. Preview 5 plans to upgrade the .NET 8 Linux build with clang 16 to maintain the Linux love for Arm32, Arm64, and x64 architectures.
System.Text.Json: Deserialize Like a Pro
Another feature is, System.Text.Json unlocks deserialization onto read-only fields and/or properties. Just tweak the PreferredObjectCreationHandling
on JsonSerializerOptions
like this:
JsonSerializerOptions options = new()
{
PreferredObjectCreationHandling = JsonObjectCreationHandling.Populate
};
This makes deserialization more efficient and friendly with read-only properties or fields.
System.Text.Json: Unleash the Power!
In .NET 8 Preview 4, System.Text.Json levels up with new features like IsReflectionEnabledByDefault
, TypeInfoResolverChain
, and support for unspeakable types.
Plus, it says bye to JsonSerializerOptions.AddContext
and welcomes a Try-
version of GetTypeInfo
. All these upgrades make JSON handling even more powerful and convenient.
Codegen: Consecutive Register Allocation FTW
.NET 8 Preview 4 introduces “consecutive register” allocation in RyuJIT’s register allocator. The algorithm’s been tweaked to make this happen, making your codegen for Arm64 even more efficient.
Debugging on Visual Studio for Mac Just Got Better!
.NET 8 Preview 4 delivers a new and improved debugging experience for Visual Studio for Mac users. The new debugging engine boosts performance and reliability, with faster stepping, a more precise call stack, and smoother handling of multi-process and multi-threaded debugging.
Note that this new engine is only available for .NET Core 3.1, .NET 5, and later projects.
Microsoft.Extensions.Logging Updates
The Microsoft.Extensions.Logging library in .NET 8 Preview 4 now supports scopes and structured logging. Create log scopes using the LoggerMessage.DefineScope
method to capture structured data without any additional memory allocation.
The new API is also compatible with the Log
methods provided by the LoggerMessage
class for a consistent logging experience.
Conclusion
So there you have it, folks! We’ve dived into some thrilling new features and enhancements in .NET 8 Preview 4, shining the spotlight on System.Text.Json improvements, register allocation in codegen, and unspeakable types support. These updates showcase Microsoft’s dedication to equipping devs with potent and streamlined tools for crafting top-notch applications. Happy coding!
Top comments (2)
Thanks for the comprehensive summary.
These updates look great. And finally an abstract time class! I've been creating an
ITimeProvider
interface in practically all of my projects (that have needed time based unit tests) for years.Don't forget all the much-needed stability improvements and fixes in the .NET MAUI platform too :-)