New OFFICIAL .NET 7 Features Released (Now FASTER and LIGHTER⚡)
Tags: csharp, dotnet, programming, beginners
Published: 2022-04-25 11:07:00

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](https://devblogs.microsoft.com/dotnet/announcing-dotnet-7-preview-3/#what-is-native-aot):

> "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](https://devblogs.microsoft.com/dotnet/announcing-dotnet-7-preview-3/)):

![Speed comparison (Source: Microsoft)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sbsjzfz3q33vns8lo57o.png)

- Size comparison (Source: [Microsoft](https://devblogs.microsoft.com/dotnet/announcing-dotnet-7-preview-3/)):

![Size comparison (Source: Microsoft)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hknq6zhina5pnazhllnv.png)

---

## System.Composition.Hosting

Apart from Native AOT, we have many more new features such as the **[Managed Extensibility Framework update:](https://devblogs.microsoft.com/dotnet/announcing-dotnet-7-preview-3/#system-composition-hosting)** Now the new APIs will allow you to add a single object instance to the `System.Composition.Hosting` container:

```csharp
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);
    }
}
```
You can check the original proposal: **[Inject existing object into MEF2](https://github.com/dotnet/runtime/issues/29400)**

---

## Observability

Microsoft also brings us improvements in support for the cloud-native specification (**[OpenTelemetry](https://opentelemetry.io/)**). Although it is still under development in .NET 7, it has been added [Allow samplers to modify tracestate](https://github.com/open-telemetry/opentelemetry-specification/pull/988) and [Samplers should be allowed to modify tracestate](https://github.com/open-telemetry/opentelemetry-dotnet/issues/1708). Here we can see the Microsoft example:

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

---

## 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](https://github.com/dotnet/runtime/pull/65738) we have seen an **improvement in startup time** that, according to Microsoft, is **[up to 10–15%.](https://devblogs.microsoft.com/dotnet/announcing-dotnet-7-preview-3/#startup-time-improvements-with-write-xor-execute-enabled)**

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](https://devblogs.microsoft.com/dotnet/announcing-dotnet-7-preview-3/#cryptography-generating-x-500-names-more-robustly) (simple literal or string formatted). This way:

```csharp
request = new CertificateRequest($"CN={subjectName},OU=Test,O=""Fabrikam, Inc.""", ...);
```

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](https://github.com/dotnet/runtime/issues/44738)). There would be no problem since each method can only operate on a single **RDN** ([Relative Distinguished Name](https://docs.oracle.com/cd/E19182-01/820-6573/ghusi/index.html#:~:text=A%20Relative%20Distinguished%20Name%20(RDN,RDN%20dc=sun.com.&text=RDN%20describe%20the%20partial%20path,another%20entry%20in%20the%20tree.)).

Let's look at the Microsoft example:

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

request = new CertificateRequest(nameBuilder.Build(), ...);
```

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.](https://devblogs.microsoft.com/dotnet/announcing-dotnet-7-preview-3/).