ASP.NET has quite some years on its shoulders now. Fourteen years to be precise. I only started working with ASP.NET in 2008, but even that is already 12 years ago. Since then the framework went through a steady evolutionary change and finally led us to its most recent descendant – ASP.NET Core 1.0.
ASP.NET Core 1.0 is not a continuation of ASP.NET 4.6. It is a whole new framework, a side-by-side project which happily lives alongside everything else we know. It is an actual re-write of the current ASP.NET 4.6 framework, but much smaller and a lot more modular.
Microsoft keep updating their ASP.NET Core, the latest I used in ASP.NET Core 3.1. In past Nov 2020, newest ASP.NET 5 has been launched and it come with new features. Here’s a one-stop resource that provides a high-level view of those changes in different areas — networking, diagnostics, general performance, code interop, regex and more — along with links for those wanting to dig into the nitty-gritty coding details.
Why You Should Migrate to Latest ASP.NET 5
1. Successor of Various .NET Versions
.NET 5 provides a common set of APIs that aligns the different runtime implementations. This set of APIs is identified by the net5.0
Target Framework Moniker (TFM), which is the token you set in your .NET project to specify the target framework. This enables your application to run on any runtime implementation supporting .NET 5. However, you can still build applications for a specific platform. For example, to build an application that uses Windows API, you need to specify the net5.0-windows
TFM. This way, building a platform-specific application is your choice, not a choice depending on the runtime implementation you are using to develop your application.
Of course, achieving this unified platform required a significant effort and an internal architecture rearrangement. A few features have been removed from the core API set, as you will see later on, but the platform gained a general improvement in performance.
While the new .NET 5 comes with the platform unification goal, the initial plan changed because of COVID-19. In fact, .NET 5 sets the foundations of the unification, but it will be completed with .NET 6 in November 2021. With that release, you will get the stable release of the new Universal UI and support for the specific TFMs for Android (net6.0-android
) and iOS (net6.0-ios
), for example.
2. New Features in C#
The second thing you should know is about C#. .NET 5 comes with C# 9, the new version of the .NET platform’s main programming language. There are several new features, but here you will find a taste of the most relevant ones.
Top-level statements
Among the new features, one of the most notable is the introduction of top-level statements. To learn what they are, take a look at the following classic minimal program:
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Just to write one string to the console, you need to define a namespace, a class, and the static method Main()
. Now, you can get rid of this code infrastructure and simply write the following:
System.Console.WriteLine("Hello World!");
Top-level statements allow you to focus on what really matters in small console programs and utilities and use C# with a more scripting-oriented approach.
Record types
Another interesting new feature is record types. With records, you can declare an immutable reference type, i.e., a class-based type that cannot be changed after its creation. An example of a built-in immutable reference type is the System.String
class. After you create an instance of a System.String
, you cannot change its value anymore.
Consider the following record type declaration:
public record Person
{
public string FirstName { get; }
public string LastName { get; }
public Person(string first, string last) => (FirstName, LastName) = (first, last);
}
You can create an instance of the Person
record as you do with a class, but you can’t change its FirstName
property:
var person = new Person("John", "Doe");
person.FirstName = "Jack"; //throws an error
However, you can compare two instances of the Person
record similarly to primitive values:
var person = new Person("John", "Doe");
var anotherPerson = new Person("John", "Smith");
Console.WriteLine(person == anotherPerson); //false
Init setters
C# 9 also adds the init
accessor to define properties that can only be initialized. To better explain its use cases, consider the following class definition:
public class Person {
public string FirstName { get; init; }
public string LastName { get; init; }
public string Address { get; set; }
}
This class defines a person with LastName
and FirstName
properties that can be initialized, but not changed. The Address
property can be changed at any time:
var person = new Person {
FirstName = "John",
LastName = "Doe",
Address = "174 Conch Street, Allera Bottom, Deep Blue Sea"
}
person.Address = "17 Shark Fish";
person.FirstName = "Jack"; //throws error
If you want to learn more about the features brought by C# 9, please check out the official documentation.
3. Multi-platform App UI
As the third thing, you have to know that .NET 5 is bringing to you a new way to build cross-platform user interfaces. With .NET Multi-platofrm App UI (.NET MAUI), you will be able to build user interfaces for Android, iOS, macOS, and Windows with a single project.
.NET MAUI can be considered an evolution of Xamarin.Forms, the open-source framework for building iOS and Android apps with a single .NET codebase. But this new framework proposes a universal model for building UIs on mobile and desktop platforms.
In addition to the well-known Model-View-ViewModel (MVVM) pattern, .NET MAUI supports the new Model-View-Update (MVU) pattern. This is a one-way data flow pattern inspired by the Elm programming language architecture that provides an effective way to manage UI updates and application state.
4. Supporting Single-File Applications
The fourth thing you will get in .NET 5 is the support of single-file applications, that is, applications published and deployed as a single file. That means that your application and all its dependencies are bundled into one file. For example, say you run the following command within the folder of your .NET 5 project:
dotnet publish -r linux-x64 --self-contained true /p:PublishSingleFile=true
You will get a single file containing your application built for Linux, all the dependencies you used in your project, and the .NET runtime (--self-contained true
). This means that you even don’t need to install the .NET runtime on the target machine.
Of course, you can also specify these parameters in your project configuration:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<!-- Enable single file -->
<PublishSingleFile>true</PublishSingleFile>
<!-- Determine self-contained or framework-dependent -->
<SelfContained>true</SelfContained>
<!-- The OS and CPU type you are targeting -->
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
</PropertyGroup>
</Project>
Be aware that this feature doesn’t use the same approach as the single-file applications you can build in .NET Core 3.1. In .NET Core 3.1, the single-file application is just a way to package binaries. At run time, they are unpackaged to a temporary folder, loaded, and executed. In .NET 5, the single-file application has a new internal structure, and it is directly executed with no performance penalty.
5. .NET 5 Won’t Longer Support This Technologies
The last of the five things you are learning about .NET 5 concerns what is no longer supported. As said above, the architectural review and the attempt to make .NET 5 an actual cross-platform programming framework led to removing a few features supported in .NET Framework. Let’s take a quick look at the removed features and the possible alternatives.
Web Forms
For a long time, ASP.NET Web Forms has been the main technology to build dynamic web UIs. However, it is not a secret that its lifetime was closely bound to .NET Framework’s destiny. .NET Core doesn’t support Web Forms, so the fact it is no longer supported in .NET 5 shouldn’t actually be news.
However, you have a few alternatives to build web UIs. If you are building traditional web applications, Razor Pages are one of these alternatives. If you want to build single-page applications, you can use Blazor.
Windows Communication Foundation (WCF)
Even WCF, the traditional communication framework for Windows, is going to be deprecated. This may appear a bit shocking for the developers that have used it to build their service-oriented applications. However, it is pretty understandable if you realize that the primary goal of .NET 5 is becoming a cross-platform framework.
The alternative to WCF recommended by Microsoft is to migrate to gRPC. But if you are nostalgic about WCF or want to prepare a smooth transition, you can give the CoreWCF open-source project a try.
Windows Workflow Foundation
Finally, .NET 5 will not even include Windows Workflow Foundation, the workflow engine technology available in .NET Framework. There is no official replacement for this technology. However, you can use an open-source porting project, CoreWF, to attempt moving your existing workflows on .NET 5 or creating new ones.
Summary
Well, that’s several reasons that you might consider why you need to migrate to ASP.NET 5. .NET 5 will impact your existing .NET projects and which opportunities the new platform is going to offer you. Maybe these highlighted five things may seem like little stuff, but they should allow you to find your way around this turning point in the .NET evolution.