.NET 8 is on the Way! 7 Features that will blow your mind đ€Ż
ByteHide
Posted on October 18, 2023
Howdy, fellow developers! Hereâs another exciting journey that weâre about to embark on, exploring the brand new features and updates released in .NET 8 Release Candidate 1 (RC 1). As the seasoned C# developer I am, I canât wait to dive into the nitty-gritty details this update has to offer.
From cross-building Windows apps on non-Windows platforms with Win32 resources to enabling hardware acceleration in WPF applications accessed via Remote Desktop Protocol, trust me, thereâs a lot here to propel your creativity and efficiency to a whole new level.
Are you ready to decode this beast?
Well, stay tuned and letâs take reap the benefits of these innovations together!
Enhancements in System.Text.Json
Letâs get to the core stuff and explore whatâs new within the System.Text.Json namespace.Youâre going to love these exciting additions! Be sure to keep up, weâre going to dive into the latest improvements in the .NET 8 Release Candidate 1 that pertain to System.Text.Json.
IAsyncEnumerable Stream Support in System.Net.Http.Json
Isnât it fabulous how developments never cease? RC1 introduces extension methods for streaming deserialization of IAsyncEnumerable.
Letâs take time to appreciate this by looking at a little code snippet, shall we?
// Setup the request URI with the placeholder "Example" website
const string RequestUri = "https://api.example.com/shoes";
// create a new HTTP client
using var client = new HttpClient();
// create an IAsyncEnumerable that will fetch Shoe data from the API asynchronously
IAsyncEnumerable<Shoe> shoes = client.GetFromJsonAsAsyncEnumerable<Shoe>(RequestUri);
// asynchronously iterate over the collection of shoes
await foreach (Shoe shoe in shoes)
{
// write the name of each shoe to the console
Console.WriteLine($"Just checked out the fabulous '{shoe.name}'");
}
// define a record that represents a shoe with its properties
public record Shoe(int id, string name, string brand, int releasedYear);
Itâs just like magic, isnât it?
Creation of JsonContent via JsonContent.Create
The power of JsonContent.Create
has taken the limelight and how! Now, you can create JsonContent
instances using trim safe/source generated contracts.
Hereâs a little demo just for you.
// create a new instance of Shoe with specified properties
var shoe = new Shoe(id: 17, "Air Max", "Nike", releasedYear: 2024);
// convert the shoe object to HttpContent using JsonContent.Create() method and the context for Shoe
HttpContent content = JsonContent.Create(shoe, MyContext.Default.Shoe);
// define a record that represents a shoe with its properties
public record Shoe(int id, string name, string brand, int releasedYear);
// define a class that includes attribute to make Shoe json serializable.
[JsonSerializable(typeof(Shoe))]
public partial class MyContext : JsonSerializerContext
{
}
Parse JsonNode Instances with JsonNode.ParseAsync
One of the top highlights in their array of features is the newly introduced capability of parsing JsonNode
instances from streams.
The process of data parsing, traditionally a complicated concept to grasp, has now been made simpler and smoother with our cutting-edge mechanism that can parse JsonNode
instances directly from streams.
The excitement this feature is garnering isnât surprising when you consider its potential to streamline the parsing process, an advantage clearly discernible to anyone who takes a moment to study the code.
// Open the shoe file with read-only access
using var stream = File.OpenRead("myShoeCollection.json");
// Parse the content of the shoe file asynchronously into a JsonNode
JsonNode node = await JsonNode.ParseAsync(stream);
JsonSerializerOptions.MakeReadOnly(bool populateMissingResolver) Enhancement
You know the good old JsonSerializerOptions.MakeReadOnly()
method, right? Well, there have been a few tweaks to make it more trim conservative.
But donât just jump right into using it, thereâs a tiny change. Itâll now throw an exception if the options arenât kitted out with a resolver. Yeah, you heard it right! No resolver? Youâre crashing with an exception!
Okay, just to stick with our shoe theme, letâs think of this as tailoring a cool shoe design when weâve got a final production run. Weâre adding some comments to explain and keep on track. Here it is:
// Begin Property Group. Think of this as starting a new design for our shoe line
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
// This is our final production run, analogous to the 'Release' phase
// In this phase, we want to apply the AndroidStripILAfterAOT. Imagine it as a final touch on our shoe design - trimming away the extras!
<AndroidStripILAfterAOT>true</AndroidStripILAfterAOT>
</PropertyGroup> // End the Property Group. Our shoe design is complete!
With this, your brand new cool shoe design is ready for the final production run! Itâs been designed, tested, and finally ready to be strutted on the fashion runway.
Done this way, if thereâs no resolver, the method will say âNo problem buddy! Iâve got your back!â And bam, itâll assign the standard reflection resolver to the option. Itâs like a safety net in case you forgot to specify a resolver.
AndroidStripILAfterAOT mode on Android
Wondering whatâs new when it comes to Android application development in .NET? Hold on to your hats, folks, because Iâm about to share something game-changing! Come on board as we explore the AndroidStripILAfterAOT
setting on Android.
Do you know what happened with .NET 6 and onwards? They implemented this cool thing called Profiled Ahead-of-Time (or AOT, if you want to sound all techy) compilation mode. Itâs pretty sophisticated: whenever youâre building an application in âReleaseâ mode, it automatically starts using this new mode.
So why the big fuss, you ask? Well, let me tell you, itâs because of the huge improvements it brings to the table. For starters, your app starts running like greased lightning, and the performance during runtime is just phenomenal. Itâs like upgrading from a tricycle to a Ferrari!
But, hang on a minute, before you start popping champagne, thereâs a small catch. When AOT compilation is put to use, the size of your application tends to pump up. You might think of it as a trade-off, but itâs not necessarily a bad thing. Size doesnât always matter, right?
Well, our saviour here is the specific way the profiled AOT works â it doesnât just go compiling everything left, right, and centre. Instead, it chooses specific bits of your appâs launch path to compile.
This method accelerates startup times while only slightly expanding the application size. So, how can one tackle this issue of inflated size? The solution lies in AndroidStripILAfterAOT
, a recent addition to the .NET functionalities.
It reduces unused Intermediate Language (IL) that was compiled using AOT, and therefore decreases the application package (apk) size. This can result in a significant size reduction between 0 and 3.5% for an application built from a dotnet template application.
When and why you should use AndroidStripILAfterAOT:
Letâs face it, when developing Android applications, performance often takes precedence over everything else. If you are willing to allow for a slight uptick in app size in favor of enhanced performance, taking the AOT-compilation route for all the code is a smart move. The introduction of $(AndroidStripILAfterAOT)
advances this approach a step further.
This feature eliminates the unused Intermediate Language (IL), making any surge in app size due to AOT-compilation more manageable. An added advantage is that the removal of this unused IL can bolster your applicationâs .NET code security.
However, keep in mind that although it strengthens the code, it does not make it completely secure against reverse-engineering tactics.
AndroidStripILAfterAOT usage:
Want to test drive it yourself? Incorporate the following MsBuild property for your Android app:
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<AndroidStripILAfterAOT>true</AndroidStripILAfterAOT>
</PropertyGroup>
By setting AndroidStripILAfterAOT
to true, youâll override the existing AndroidEnableProfiledAot
setting and all trim-able AOTâd methods get shown the door. Profiled AOT and IL stripping can work in harmony when setting both explicitly:
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<AndroidStripILAfterAOT>true</AndroidStripILAfterAOT>
<AndroidEnableProfiledAot>true</AndroidEnableProfiledAot>
</PropertyGroup>
Now, letâs compare apk sizes for a dotnet new android
app under different configurations:
For the record, when you have $(AndroidStripILAfterAOT)
set to false and $(AndroidEnableProfiledAot)
set to true, youâre going with the default Release configuration environment and your app will stand at 7.7MB.
A Closing ThoughtâŠ
So, excited to get stuck into these new features? I sure am! It would be great to get your feedback. If you stumble on any bugs or issues, please file them directly at the dotnet/runtime repository. Your input will help us make this feature even more user-friendly. Start exploring and letâs keep improving together!
Change in Tagging Pattern for .NET Containers
As we get closer to the General Availability (GA) release of .NET 8, an essential amendment has taken place in the .NET container images. Ready for whatâs new? Say goodbye to the âpreviewâ in the tag name for RC 1 and weâll walk you through the changes.
In the previous preview versions of .NET 8, we published the floating tags as 8.0-preview
and 8.0-preview-<OS>
. Well, from the RC 1 release onwards, we wonât maintain these tags. You may now be wondering what you should do instead. Well, simply replace your tag references with 8.0
and 8.0-<OS>
. Sound easy, right?
Whatâs the rationale behind the change, you ask? This transition ensures a smooth sailing journey towards the GA release of .NET 8. Moreover, these are set to be the permanent tags maintained throughout the lifecycle of .NET 8.
Non-Windows Platforms can Now Cross-Build Windows Apps with Win32 Resources
Do you happen to create Windows apps from non-Windows platforms? This is going to be your favorite. Now imagine this, youâre building applications meant for Windows but on non-Windows platforms.
Any recognized Win32 resources get seamlessly integrated into the resulting executable. A sigh of relief for cross-building aficionados, isnât it?
Weâre talking about things like your application icon, manifest, and version information. Cool, right?
In the past, to get these resources, applications had to be built on Windows. Eliminating this gap in cross-building support was a high demand, as it majorly alleviated issues related to infrastructure complexity and resource usage. Goodbye to those pain points and say hello to more efficient cross-building!
Azure Managed Identity Now Supported for SDK Container Publishing
Alright everyone, sit tight because Iâve got some groundbreaking news that will make your developer hearts sing! Remember how it used to be such a task to convert .NET applications to containers and then pushing these containers off to registries like Docker Hub and Azure Container Registry? Well, those days are over, my friends, thanks to the fantastic SDK Container publish feature!
Now, Iâm sure youâre no stranger to the fun and games we usually have when it comes to authenticating this sort of procedure. Most of the time, weâre stuck using that docker login
command, right?
However, there was always a catch when dealing with certain registries like Azure Container Registry â they arenât into ordinary usernames & passwords. No siree, they instead kick it up a notch and use OAuth token exchange for authentication. Unfortunately, our poor old SDK Container publishing tools struggled to deal with this level of sophistication.
Letâs get the confetti out because our long-awaited hero is here â the .NET 8.0.100 RC1! Starting today, our SDK Container publishing tool is totally cool with OAuth token exchange authentication method! Yeah, hard to believe, isnât it? Finally, Azure Container Registry and other registries using the same method are now fair game!
> az acr login -n <your registry name>
> dotnet publish -r linux-x64 -p PublishProfile=DefaultContainer
WasmStripILAfterAOT Mode on WASM
Any Blazor WebAssembly aficionados out there? If you use often WASM Browser, this oneâs for you! Both platforms now support ahead-of-time (AOT) compilation where you can directly compile your .NET code into WebAssembly.
AOT compilation does come with its perks â it boosts runtime performance. But, as a caveat, it also results in larger app sizes. With the new WasmStripILAfterAOT
mode, we can slice the size of the _framework folder by a nifty 1.7% â 4.2%. If youâre curious about how we arrived at these figures, check out the above Github issue.
Is WasmStripILAfterAOT Right for You?
Trust me when I say, this feature is a superstar whenever you go for AOT compilation. Smaller app size, and same stellar performance? Yeah, give it a shot!
Get Started with WasmStripILAfterAOT:
Ready to whip this tool into action? Set the following MsBuild property for your Blazor WebAssembly or WASM Browser app:
<PropertyGroup>
<RunAOTCompilation>true</RunAOTCompilation>
<WasmStripILAfterAOT>true</WasmStripILAfterAOT>
</PropertyGroup>
By using this command, a large portion of the Intermediate Language (IL) code from both the built-in libraries and the code written by you splendid application developers will be chopped off. Itâs like a neat little trim for your code to make it sleeker and more efficient.
WPF Hardware Acceleration in Remote Desktop Protocol
Hey there, WPF fans! Have you ever been working remotely, using a WPF application, and thought: âMan, I wish this thing had sharper visuals?â Well, itâs your lucky day because weâve got just the update youâve been dreaming of!
Back in the day, you used to only have the option of software rendering, even when accessing your WPF applications remotely. Remember how frustrating that was? Youâd have a beast of a machine capable of hardware rendering, but still being forced to settle for software rendering⊠a total bummer!
Well, forget all that because weâre stirring things up with an amazing new feature that empowers developers to leverage the pure power of hardware acceleration when using the Remote Desktop Protocol (RDP). No more limitations, and no more settling for less. You just flip an AppContext switch and voilĂ , you are all set!
Scratching your head wondering what the heck this hardware acceleration gibberish is? Let me break it down for you. Hardware acceleration is when we let your computerâs graphic processing unit (GPU) take the reins. This GPU speeds up the creation of visuals and graphics within your WPF application.
So, what can you expect? A smooth, captivating visual experience that looks sweet and runs buttery smoothâquite the upgrade from software rendering, where your computerâs central processing unit (CPU) did all the heavy lifting. This old-school method was slower, not super efficient and letâs be honest, a total vibe killer.
Hardware Acceleration in RDP for Your WPF App:
Alright, letâs get down to the nitty-gritty. How do you actually switch on this hardware acceleration feature in RDP? Well, my friends, you have not one but two ways to get this done, because who doesnât like a little flexibility and choice, right?
- The first way is by introducing the
RuntimeHostConfigurationOption
into your handy dandy*.csproj
file, as demonstrated in the following code snippet.
<ItemGroup>
<RuntimeHostConfigurationOption Include="Switch.System.Windows.Media.EnableHardwareAccelerationInRdp" Value="true" />
</ItemGroup>
- By adding the
configProperty
in the*.runtimeconfig.json
file, as illuminated below:
"configProperties": {
"Switch.System.Windows.Media.EnableHardwareAccelerationInRdp": true
}
Last Bit of Wisdom:
Gear up, folks, and test drive this fresh-off-the-lab feature. And should you stumble upon any glitches along the way, donât hesitate to reach out. Got a bug to report or feedback to give? Send them directly to our dotnet/wpf repository. Weâre eager to hear from you, because together, weâre only going to make the experience even better!
Fast Recap
So there you have it, a deep dive into the world of .NET 8 Release Candidate 1. I hope you found this useful and picked up a trick or two to implement in your projects. Remember, innovation and updates are always part of the developerâs lifecycle. Keep exploring, keep learning, and donât forget to have fun while coding!
Till next time, keep your code clean, your bugs minimal, and your passion for coding aflame. Happy coding, folks! Remember, the game is about continuously improving, adapting to changes and staying ahead â and there is no better way to do that than embracing every new update that comes your way.
So, stay tuned for more engaging content, updates, and development adventures.
Posted on October 18, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.