left-icon

.NET 7 and C# 11 Succinctly®
by Dirk Strauss

Previous
Chapter

of
A
A
A

CHAPTER 1

Introducing .NET 7

Introducing .NET 7


To understand how we arrived at .NET 7, we should first briefly examine the history of .NET in general.

The History of .NET

Figure 1: The History of .NET

.NET Core 1.0 was released in 2016 and heralded the start of what we know today as .NET 7. Before this release, developers were accustomed to the .NET Framework first released in February 2002. It was closed-source and only ran on Windows. .NET Core, however, was the first .NET version to run across platforms and is an open-source project.

.NET Core saw many releases between 2016 and 2018, culminating in the release of .NET Core 3.1 in 2019. .NET Core 3.1 was a long-term support version, with support for it ending in December 2022.

Many companies developed software on .NET Core 3.1 due to its long-term support status. The other day, I ran into a project developed on .NET Core 3.1 that I had to upgrade to .NET 7. It was, therefore, a very popular version of .NET Core. Then, in 2020, Microsoft surprised us all with the release of .NET 5.0. (version 4 was skipped to avoid confusion with the .NET Framework versions.) Microsoft touted .NET 5.0 as a single, unified platform that would replace .NET Core, Xamarin, and the .NET Framework. Support for .NET 5.0 ended in May 2022.

In November 2021, Microsoft released .NET 6.0, with long-term support until December 2024. This introduced a host of new features and unified the various .NET platforms into a single .NET version.

The standard-term support release of .NET 7.0 in November 2022 brings us to where we are today: a unified .NET that supports cross-platform application development, allowing developers to create a host of different applications—from desktop apps to cloud apps, web applications, Unity games, mobile applications, and AI apps.

The unification of .NET means that we now have a single BCL (or base class library) and SDK. With the release of MAUI, we can create cross-platform native UI applications.

About .NET 7

Compared to .NET 6.0, there were fewer changes with the release of .NET 7. It did, however, bring with it performance increases. A thousand performance-impacting pull requests went into the runtime and core libraries, resulting in developers often gaining a significant performance boost in their applications by simply upgrading to .NET 7.0. These performance improvements make .NET 7.0 the fastest version of .NET thus far.

This, alone, is reason enough to upgrade to .NET 7.0. New APIs have also been introduced in .NET 7.0. Changes include:

·     Microseconds and nanoseconds were added to DateTime.

·     Changes have been made to the Microsoft.Extensions.Caching namespace.

·     Support for working with Tar (tape archive) files has been included with System.Formats.Tar.

·     Various options were also added to System.Text.Json.

ASP.NET Core also had notable changes. These include additions such as rate limiting, allowing ASP.NET Core applications to limit the requests they handle in a specific amount of time. Output caching has also been added and configures how responses from the ASP.NET Core application are cached.

Minimal APIs, introduced in .NET 6.0, also received an update, including endpoint filters and route groups.

Introduced in early 2022, MAUI has also received some love. MAUI allows developers to build mobile applications targeting iOS and Android, Windows desktop, and macOS. With .NET 7.0, one of the key improvement areas to MAUI has been performance. MAUI also received some new controls, such as the map control. Desktop targeting has also been improved.

Some of the tooling improvements include Azure support, container support, hot reload, and CLI improvements. Microsoft has also made improvements to the upgrade assistant, allowing developers to upgrade their applications to the latest version of .NET. While it is by no means perfect, it significantly assists developers with going through the pain of upgrading older .NET applications.

.NET 7.0 is a standard-term support release, and it is supported for six months after the release of .NET 8.0. The result is that any production application running .NET 7.0 must be upgraded when .NET 8.0 is released in November 2023.

Upgrading existing applications to .NET 7

By now, it should be clear that moving to .NET 7.0 is wise if you want to use the latest features, let alone utilize the speed enhancements that .NET 7.0 brings.

Luckily for us, upgrading an existing .NET 6.0 project to .NET 7.0 is quite simple, as illustrated in the following code listings.

If you view the project file of an application built on .NET 6.0, you see that the TargetFramework element is set to the value net6.0.

Code Listing 1: A .NET 6.0 Project File

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>

    <OutputType>Exe</OutputType>

    <TargetFramework>net6.0</TargetFramework>

    <ImplicitUsings>enable</ImplicitUsings>

    <Nullable>enable</Nullable>

  </PropertyGroup>

</Project>

Modify the project file to reference net7.0 to make use of .NET 7.0.

Code Listing 2: A .NET 7.0 Project File

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>

    <OutputType>Exe</OutputType>

    <TargetFramework>net7.0</TargetFramework>

    <ImplicitUsings>enable</ImplicitUsings>

    <Nullable>enable</Nullable>

  </PropertyGroup>

</Project>

Remember to upgrade any other NuGet packages to their .NET 7.0 counterparts, also.

It is worth noting that since .NET 6.0, the Startup class has been removed, and the Program.cs file also now uses top-level statements. These changes are optional, however. (I guess I’m an old man, because I am not too fond of these changes and still favor having a Startup class.)

It’s also rather apparent that the older the project that you are upgrading, the more work you have to do to upgrade it. Upgrading a project developed with .NET Core 1.x, for example, requires much more effort, but it is certainly doable.

Tip: It is worth noting that using the .NET Upgrade Assistant is an excellent choice for larger projects. You can download the .NET Upgrade Assistant from this URL.

Unfortunately, if your project is built on the .NET Framework, you have much more work to do. There is no direct upgrade path, meaning you must create a new project and move over the files manually.

Let’s have a closer look at the .NET Upgrade Assistant.

The .NET Upgrade Assistant

The .NET Upgrade Assistant is an upgrade tool that assists developers in upgrading their applications to the latest version of .NET. It can also migrate your projects from older platforms, such as Xamarin Forms and UWP, to newer offerings. To get started with the .NET Upgrade Assistant, you can install the Visual Studio Extension or install it from the .NET global tool. Let’s look at the Visual Studio Extension first.

Installing the Visual Studio extension

Installing the Visual Studio Extension is done from within Visual Studio. From the Extensions menu, click Manage Extensions.

Manage Extensions

Figure 2: Manage Extensions

In the Manage Extensions window that is displayed, search for the .NET Upgrade Assistant.

The Upgrade Assistant Extension

Figure 3: The Upgrade Assistant Extension

Download the .NET Upgrade Assistant and restart Visual Studio. The process of installing the tool begins, displayed in the VSIX Installer, as seen in Figure 4.

Installing the .NET Upgrade Assistant

Figure 4: Installing the .NET Upgrade Assistant

Once you install the .NET Upgrade Assistant, you can open your project and start the upgrade process. In my example project, I have a web application for an ice cream parlor’s online store that I developed in ASP.NET Core 2.1.

Code Listing 3: My Ice Cream Parlor Project File

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>

    <TargetFramework>net472</TargetFramework>

    <DebugType>full</DebugType>

  </PropertyGroup>

  <ItemGroup>

    <PackageReference Include="Microsoft.AspNetCore" Version="2.1.7" />

    <PackageReference Include="Microsoft.AspNetCore.CookiePolicy" Version="2.1.14" />

    <PackageReference Include="Microsoft.AspNetCore.HttpsPolicy" Version="2.1.1"/>

    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.3" />

    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.1.1" />

  </ItemGroup>

</Project>

You can see that it targets the .NET Framework 4.7.2 because I checked the option to run on .NET Framework when creating the project.

Additional Information Selected When Creating Project

Figure 5: Additional Information Selected When Creating Project

To start upgrading, I right-click my project in the Solution Explorer and select Upgrade from the context menu.

Upgrade a Project

Figure 6: Upgrade a Project

Selecting Upgrade from the context menu displays the Upgrade Assistant and gives me the option of an in-place project upgrade.

The Upgrade Assistant supports three upgrade types. These are:

·     In-place

·     Side-by-side

·     Side-by-side incremental

I will expand a bit more on the three options available later in this chapter. For now, I will click the In-place project upgrade option.

The Upgrade Assistant

Figure 7: The Upgrade Assistant

The Target Framework Selection

Figure 8: The Target Framework Selection

The Upgrade Assistant now asks me which target framework I want to upgrade to. As this book is about .NET 7.0, I will select it from the available options. In reality, if your project is already in production or due to be released soon, you might want to select .NET 6.0 to take advantage of the long-term support.

Please make no mistake, though: .NET 7.0 is a perfectly reasonable choice and, as mentioned before, brings performance improvements and many other benefits.

The Components to Be Upgraded

Figure 9: The Components to Be Upgraded

The .NET Upgrade Assistant now displays the components it will upgrade. On this screen, you can check and uncheck the components as required. The selections you make here might take some consideration when dealing with large solutions containing many projects.

As my project is small, I keep the default selections and click the Upgrade selection button.

The Upgrade Process

Figure 10: The Upgrade Process

The .NET Upgrade Assistant now starts the upgrade process and displays a progress bar.

The Completed Upgrade Process

Figure 11: The Completed Upgrade Process

Once the upgrade process completes, you are shown a summary of the upgrade results with how many components succeeded, failed, or were skipped.

The Upgrade Result

Figure 12: The Upgrade Result

You will also see the results listed for each component in the window below the Upgrade Assistant. Hover over each check icon to see a descriptive tooltip.

If you compare the project file from before the upgrade process (Code Listing 3) to the project file after the upgrade process (Code Listing 4), you will notice that a lot has changed.

Code Listing 4: The Upgraded Project File

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>

    <TargetFramework>net7.0</TargetFramework>

    <DebugType>full</DebugType>

  </PropertyGroup>

</Project>

Upgrading your projects using the .NET Upgrade Assistant provides a much quicker experience than manually changing references and package dependencies.

Installing the .NET global tool

Installing the tool is done by running the following command in the .NET CLI.

Code Listing 5: The .NET CLI Command

Because the .NET Upgrade Assistant is installed as a .NET tool, you can update it easily by running the following command.

Code Listing 6: Updating the .NET Upgrade Assistant

dotnet tool update -g upgrade-assistant

With the tool installed, navigate to the directory containing your project that needs to be upgraded and call the following command.

Code Listing 7: Upgrading a Visual Studio Project

upgrade-assistant upgrade

The tool now provides an interactive way of choosing the project to upgrade and a choice of which .NET version to target.

Different upgrade types

As mentioned earlier in the chapter, the .NET Upgrade Assistant supports three upgrade types. These upgrade types provide different upgrade experiences and depend on which project types are being upgraded.

In-place

This option upgrades your project all at once. Creating a separate branch for the upgraded project in your source control solution is prudent.

Side-by-side

The side-by-side option does not modify your original project. It places a copy of your project in your solution that contains the upgraded code. This is an excellent option if your project contains many dependencies that might break after the upgrade.

Side-by-side incremental

When dealing with web applications that require upgrading from ASP.NET to ASP.NET Core, this option is ideal because these two technologies are quite different. Using this upgrade type puts a .NET project next to your existing .NET Framework project. It routes endpoints implemented in the .NET project while routing all other calls to the .NET Framework application. The side-by-side incremental upgrade allows you to be in control and slowly upgrade your application bit by bit.

In conclusion

The .NET Upgrade Assistant supported C# and Visual Basic at the time of this writing. The supported projects are as follows:

·     ASP.NET

·     Azure Functions

·     Windows Presentation Foundation

·     Windows Forms

·     Class libraries

·     Console apps

·     NET Native UWP

·     Xamarin Forms

·     .NET MAUI

The upgrade paths that are supported are:

·     .NET Framework to .NET

·     .NET Core to .NET

·     UWP to WinUI 3

·     Previous .NET version to the latest .NET version

·     Azure Functions v1-v3 to v4 isolated

·     Xamarin Forms to .NET MAUI

·     XAML file transformations only support upgrading namespaces. For more comprehensive transformations, use Visual Studio 2022 version 17.6 or later.

It goes without saying that you need to test your project after an upgrade. The .NET Upgrade Assistant is by no means a magic wand, but it allows for a much better developer experience when compared to upgrading your project manually.

For more information on the .NET Upgrade Assistant, you can browse this URL.

Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.