DEV Community

Cover image for New OFFICIAL .NET 7 Features Released (Now FASTER and LIGHTER⚡)
Dotnetsafer
Dotnetsafer

Posted on

New OFFICIAL .NET 7 Features Released (Now FASTER and LIGHTER⚡)

Microsoft does not stop! For months now we have seen updates and news from the .NET development team and they have just released the third preview of .NET 7. This final version is planned for release in November this year, but until then let's talk about the new features and performance improvements it will bring!


Faster, Lighter Apps (Native AOT)

After a while, the experimental Native AOT project has become the main focus of Microsoft's development. As many of us have been asking for a long time, Microsoft has decided to bring us a couple of updates to Native AOT.

For those who don't know what Native AOT is, Ahead-of-time (simply AOT) generates code at compile-time instead of run-time.

At this time, Microsoft already offers ReadyToRun (client/server applications) and Mono AOT (mobile and WASM applications) for this purpose. In addition, Microsoft adds that Native AOT does not replace Mono AOT or WASM.

Native AOT is characterized by what its name indicates: It generates code at compile time but in Native. Its biggest advantage is the performance improvement, mainly in:

  • Startup time

  • Memory usage

  • Disk size

  • Access to restricted platforms

Microsoft explains how Native AOT works:

"Applications start running the moment the operating system pages in them into memory. The data structures are optimized for running AOT generated code, not for compiling new code at runtime. This is similar to how languages like Go, Swift, and Rust compile. Native AOT is best suited for environments where startup time matters the most."

In addition, they have revealed a benchmark comparing NativeAOT against ReadyToRun, in which the compile time is up to 73% faster and almost half as light:

Speed comparison (Source: Microsoft)

Size comparison (Source: Microsoft)


System.Composition.Hosting

Apart from Native AOT, we have many more new features such as the Managed Extensibility Framework update: Now the new APIs will allow you to add a single object instance to the System.Composition.Hosting container:

namespace System.Composition.Hosting
{
    public class ContainerConfiguration
    {
        public ContainerConfiguration WithExport<TExport>(TExport exportedInstance);
        public ContainerConfiguration WithExport<TExport>(TExport exportedInstance, string contractName = null, IDictionary<string, object> metadata = null);

        public ContainerConfiguration WithExport(Type contractType, object exportedInstance);
        public ContainerConfiguration WithExport(Type contractType, object exportedInstance, string contractName = null, IDictionary<string, object> metadata = null);
    }
}
Enter fullscreen mode Exit fullscreen mode

You can check the original proposal: Inject existing object into MEF2


Observability

Microsoft also brings us improvements in support for the cloud-native specification (OpenTelemetry). Although it is still under development in .NET 7, it has been added Allow samplers to modify tracestate and Samplers should be allowed to modify tracestate. Here we can see the Microsoft example:

//  ActivityListener Sampling callback
    listener.Sample = (ref ActivityCreationOptions<ActivityContext> activityOptions) =>
    {
        activityOptions = activityOptions with { TraceState = "rojo=00f067aa0ba902b7" };
        return ActivitySamplingResult.AllDataAndRecorded;
    };
Enter fullscreen mode Exit fullscreen mode

Reduced start-up time (Write-Xor-Execute)

As we have already seen at the beginning, Microsoft has decided to focus mainly on performance improvements and this one is no exception. Now with Reimplement stubs to improve performance we have seen an improvement in startup time that, according to Microsoft, is up to 10–15%.

This is mainly due to a large reduction in the number of modifications after code creation at runtime.


Generating X.500 names more robustly

Also in this preview, Microsoft has focused on cryptographic security. That is why building an X500DistinguishedName is now much easier and more secure.

For those who don't know this about the construction of an X.500 name, it used to be done with string manipulation (simple literal or string formatted). This way:

request = new CertificateRequest($"CN={subjectName},OU=Test,O=""Fabrikam, Inc.""", ...);
Enter fullscreen mode Exit fullscreen mode

The main problem with this is that the quote, comma or any other element influences the parser. Microsoft's solution has been to add the X500DistinguishedName class (check). There would be no problem since each method can only operate on a single RDN (Relative Distinguished Name.

Let's look at the Microsoft example:

X500DistinguishedNameBuilder nameBuilder = new();
nameBuilder.AddCommonName(subjectName);
nameBuilder.AddOrganizationalUnitName("Test");
nameBuilder.AddOrganizationName("Fabrikam, Inc.");

request = new CertificateRequest(nameBuilder.Build(), ...);
Enter fullscreen mode Exit fullscreen mode

These are the main new features of the new .NET 7 preview 3. If you want to see in depth everything that brings in its latest version, I recommend you the original source: Announcing .NET 7 Preview 3.


From Dotnetsafer we want to thank you for your time in reading this article.

And remember: Now you can try for free our .NET obfuscator. You can also protect your applications directly from Visual Studio with the .NET Obfuscator Visual Studio Extension.

Discussion (4)

Collapse
lidiaaadotnet profile image
lidiaaadotnet

Personally I think that the new .NET 7 is very very good but I don't know if they will be able to beat everything they brought in .NET 6.

Collapse
dotnetsafer profile image
Dotnetsafer Author

The only thing left to do is to wait, until November there is still plenty of time :)

Collapse
mccshreyas profile image
Shreyas Jejurkar

For .NET 7 the main theme is to improvements for minimal API's and NativeAOT!
In some cases .NET 7 is already faster than .NET 6 before even GA! A lot more to come! 😅😅

Collapse
dumboprogrammer profile image
Tawhid

Damn! Microsoft rolling out some great stuff