.NET 8 Preview 7 Features

.NET 8 Preview 7 Features
October 11, 2023
6 minutes read

Alright, devs, who’s up for some next-level tech talk? Preview 7 for .NET 8 has landed, and this is pretty much the final preview we’re looking at before the grand release. We’re going to deep-dive into what this newest kid on the block has in store for us, mainly focusing on System.Text.Json and codegen – our key game-changers. If .NET 8 hasn’t caught your eye yet, the time is now!

.NET Conf 2023: Mark Your Calendars!

Pop the champagne and toss the confetti! We’ve got the dates for .NET Conf 2023. It’s happening from November 14-16, 2023. Can’t you feel the electricity in the air already?

A release coupled with a conference? Now, that’s a double whammy of excitement! Be there to see .NET 8 in its full glory – showcased, celebrated, and thoroughly dissected.

System.Text.Json: New and Improved

Get your party hats on, people! System.Text.Json in .NET 8 Preview 7 is jam-packed with enhancements. Brace yourselves as we’re about to take a plunge into a sea of new features and improvements – all in one go!

JsonSourceGenerationOptionsAttribute – Feature Parity at Its Best

What’s the latest scoop? We now have the JsonSourceGenerationOptionsAttribute matching feature for feature with the distinguished JsonSerializerOptions class. You wonder if it’s noteworthy?

Let me assure you, it is!

This development implies we now have the ability to define serialization settings during the compilation phase. That’s quite a significant benefit, wouldn’t you agree?

Check this out:

Loading code snippet...

What’s happening here? Well, this example generates a MyContext.Default property, preconfiguration included. This means that all the related options are neatly set in place. And the cherry on top? It takes source generation to a completely new level.

Built-in Support for Memory/ReadOnlyMemory

Kudos to Microsoft for being so forward-thinking! The serializer now inherently supports Memory<T> and ReadOnlyMemory<T> types. The repercussions? Well, let me articulate this clearly: arrays are granted a whole different layer of semantics!

Check out these examples:

Loading code snippet...

Say Hello to Half, Int128 and UInt128

Strap in for some groundbreaking news – Half, Int128, and UInt128 types now enjoy built-in serialization support. And yes, it is a big deal!

Let’s see it running:

Loading code snippet...

Welcoming JsonIncludeAttribute and JsonConstructorAttribute

Prepare for enhanced versatility! The JsonInclude and JsonConstructor attribute annotations now enable the inclusion of non-public members into the serialization contract. It’s certainly a notable advancement!

Here’s an example:

Loading code snippet...

Meet IJsonTypeInfoResolver.WithAddedModifier

Here’s a fun new feature. The extension method IJsonTypeInfoResolver.WithAddedModifier lets you make modifications to the serialization contracts of any IJsonTypeInfoResolver instance. What can I say, Microsoft sure knows how to keep the party going.

Loading code snippet...

Say ‘Hi’ to the Additional JsonNode Functionality

Let’s hear it for JsonNode, which is ramped up with much-improved functionality.

For instance, it now supports deep cloning:

Loading code snippet...

It also supports IEnumerable now:

Loading code snippet...

Next-level Hosting with Microsoft.Extensions.Hosting.IHostedLifecycleService

Behold the fantastic upgrade in hosted services! With the addition of IHostedLifeCycleService, it extends your control over the application lifecycle. While IHostedService already had StartAsync and StopAsync, this new interface adds a suite of methods – StartingAsync, StartedAsync, StoppingAsync, StoppedAsync. In simple words, now you can handle events not just at start and stop, but also right before start and right after completion. How cool is that?

How to Use IHostedLifecycleService

Now, let’s dive deep and understand how to employ this newfound power. Take a quick peek at the code snippet below:

Loading code snippet...

So, there you go. With this new interface, you can now have methods running just before and after your application starts and stops. Awesome, right?

Keyed Services Support in Microsoft.Extensions.DependencyInjection

Esteemed audience, witness the integration of Keyed services support in the reliable Microsoft.Extensions.DependencyInjection. Curious about its advantages? This intelligent feature from Microsoft enables enhanced segregation of service registration and usage. Quite the timely innovation, wouldn’t you agree?

Rollin’ with Keyed Services

Now, are you all excited to know how to use it? Thought so! Let’s dig right into it.

Loading code snippet...

Ta-da! And that’s how you ace the game with Keyed Services. Enjoy scoping your services with keys and make your code cleaner and neater. Are you ready to go key-searching in your code? Now go ahead and make keys your best friends! Cheers to easy and efficient code! Let’s get our codes key-ready, shall we?

You May Also Like

Optional Parameters in C#: What You Need to Know

Optional Parameters in C#: What You Need to Know

Programming in C# often requires flexibility and simplicity ...

What is Lock Keyword in C#? Main Usages

What is Lock Keyword in C#? Main Usages

IndexUnderstanding the C# Lock Keyword Can’t tell a lock fro...

Enumerate in C#: Detailed Explanation

Enumerate in C#: Detailed Explanation

Desperate to decode the mystery that is enumeration in C#? I...

Creating a JSON Class in C#: Detailed Guide

Creating a JSON Class in C#: Detailed Guide

In the world of application development, manipulation of dat...

Static Class in C#: How to Use It?

Static Class in C#: How to Use It?

Hello there, future C# aficionado! It’s time to roll down th...

DateTime Formatting in C#: Dev Guide

DateTime Formatting in C#: Dev Guide

Have you ever gotten frustrated handling dates and times in ...

Leave a reply

Loading comment form...