Monday, March 5, 2018

Why use .NET Core

Developers have the .NET Framework and .NET Core to choose from. What are the differences and why should you use .NET Core?
Photo by Anne Nygård on Unsplash
Developers working with .NET since the beginning know what a big and important change .NET was. For those who worked with Classic ASP, Perl, Com+ and CGIs, that was a real change! But as you can expect, .NET back then wasn't what it is today. Those who worked with NET 1.1 for example may remember that back then, the framework didn't have Nullable types so developers had to implement our own INullable<T> class.

Using a new framework only makes sense if the framework accelerates, simplify the software development process and introduces access to new features. Using the latest tools and features may be awesome but can also be a big risk!

.NET Framework

But .NET 1.1 wasn't ready yet Everything changed when .NET 2.0 was released. With it, the community got access to Nullable types (yay!), Partial classes, iterators and anonymous methods and a flourishing ecosystem making it a serious and competent framework for enterprise software. As the .NET framework grew in popularity, Microsoft constantly improved it release after release. For example, my favourite features were:
As you can see, there was a significant decrease in new features after .NET 4.5. Well, if you look at the release dates, you'll see that both .NET 4.6 and .NET Core were announced in 2014. Why? Let's see next.
If you want to read more about the .Net Framework's history, please click here.

.NET Core

Microsoft introduced .NET Core to the world in the end of 2014. According to them:
.NET Core is a cross-platform development framework that can be used in servers, workstations, mobile devices, cloud and embedded/IoT devices.
As you might expect .NET Core 1.0 wasn't ready yet for production. However, it indeed showed a huge potential since for the first time, .NET developers were able to build, run and deploy their apps on Linux and Mac. Plus, with the Docker revolution, they'd be able to work on containers, Kubernetes and microservices as the rest of the world did.

But the .NET Core only grew up on version 2.1. Released 2018, .NET Core 2.1 is not only an LTS release but it brought massive API compatibility with .NET Framework:
  • Double the number of APIs from 13k in .NET Standard 1.6 to 32k in .NET Standard 2.0
  • Added support for .NET Standard 2.0.
  • Added support for referencing .NET Framework libraries and NuGet packages.
  • Made available the Linux .NET Core SDK and Runtime builds for most Linux distributions.
.Net Standard 2.0 - Source

Why use .NET Core

So, with that short introduction, let's understand why we should use .NET Core.

Cross-platform

Runs on Windows, Mac and Linux;

Container-friendly

That's a huge win for .NET Cover over .NET Framework. Because .NET Core runs natively on Linux, it

Powerful CLI

Differently from the .NET framework, NET Core can be exercised using command line tools like we're used to doing in Linux for so long. The .NET Core CLI is a powerful and very welcome addition to the .NET family. Finally!

Open Standard

.NET Core is compatible with the open-source .NET Standard, the standard for .NET-compatible platforms and created to establish greater uniformity in the .NET ecosystem. .NET Standard includes a formal specification of .NET APIs that are intended to be available on all .NET implementations including Xamarin and Mono.

Microsoft still provides the .NET implementation compatibility table:
Source: .NET Implementation Support

Open source

Being open-source is also a huge advantage. Now we can read, learn, fix and contribute to the code. For example, if you go to dotnet Core on GitHub, you'll be able to:
  • report bugs
  • participate in discussions
  • follow the project
  • follow the releases
  • read the release notes 
  • and much more!

Side-by-side versions

.NET can be installed side-by-side user- or machine-wide. That's a significant and major advancement of .NET Framework that was built before Windows 10 and Docker. Side-by-side versions allow developers (those that aren't deploying their apps on containers) to target specific versions of the framework without breaking their apps.

Improved Platform Support

Windows, Mac, Linux and Docker and soon with .NET 5,  will also run on XBox, Mobile, IoT, Cloud. In summary, everywhere.

SignalR Core

As I mentioned above, yes, the new SignalR Core is finally available with this release. That's awesome news for those who are using this library and were waiting for that upgrade!

Entity Framework Core - A new Entity Framework

Entity Framework also gained some modern features and also became open-source on its new incarnation names Entity Framework Core. EF Core includes faster queries, a serious list of database providers and support ranging from SQLite cloud databases. Plus, EF can also be installed and run from the command line via it's EF.NET CLI.
Installation is as simple as:
dotnet tool install --global dotnet-ef

Razor Class Libraries

We can now embed Razor in class libraries so Razor is used in your backend to build views and pages into reusable class libraries. According to Microsoft:
Razor views, pages, controllers, page models, Razor components, View components, and data models can be built into a Razor class library (RCL). The RCL can be packaged and reused. Applications can include the RCL and override the views and pages it contains. When a view, partial view, or Razor Page is found in both the web app and the RCL, the Razor markup (.cshtml file) in the web app takes precedence.

.NET Core Tools

This is a very important feature. Existing already in NPM, it basically allows us to install global applications from nuget packages. .NET Core tools run on all .NET Core supported operating system and chip architecture by default, with one set of binaries. To install a global .NET tool, run:
dotnet tool install --global dotnetsay

Build Improvements

Builds are faster on .Net Core 2.1. Here's a comparison from Microsoft:

.NET Core 2.1 Incremental Build-time performance improvements
Source: https://blogs.msdn.microsoft.com/dotnet/2018/05/30/announcing-net-core-2-1/

Docker Support 

Docker support is probably the biggest benefit of .NET Core to date. Docker, containers and microservices are booming and became the standard way to build, package and ship our apps. Today Microsoft offers multiple .NET Core images on  Docker Hub and creating your container is some keywords away.

ASP.NET Core

ASP.NET gained tons of features and on .NET Core is more lightweight and faster than its older sibbling including:
  • The framework is completely distributed as NuGet packages
  • Cloud-optimized runtime
  • A unified story for building web UI and web APIs
  • A cloud-ready environment-based a rewritten configuration system
  • A lightweight and modular HTTP request pipeline
  • Build and run cross-platform ASP.NET Core apps on Windows, Mac, and Linux
  • Open-source
  • Side-by-side app versioning when targeting .NET Core.

The dotnet tool also provides lots of templates so you can quickly scaffold the project you need from the command line:

Blazor

Blazor lets you build interactive web UIs using C# instead of JavaScript. Blazor apps are composed of reusable web UI components implemented using C#, HTML, and CSS. Both client and server code is written in C#, allowing you to share code and libraries. Blazor is a feature of ASP.NET, the popular web development framework that extends the .NET developer platform with tools and libraries for building web apps.

Significant Perform Enhancements

.NET Core also brings significant performance features over .NET Framework. Many are the reasons, notably an extensive use of async patterns, ligther codebase and decoupling from heavier Windows-only features such as WPF and IIS. For more information check:

Modern Features

.NET Core brings new features including gRPC, Razor Pages, Blazor, and many other updates, including a more up-to-date development framework providing simple integration with Node.js, Vue.js and React.

Kestrel

Kestrel is the default web server specified by the ASP.NET Core project templates. Kestrel can be used by itself as an edge server processing requests directly from a network, including the Internet. Kestrel can be run directly or indirectly with the Internet without a reverse proxy server, with a reverse proxy server, such as IIS, Nginx, or Apache or indirectly with the Internet through a reverse proxy server, such as IIS, Nginx, or Apache.

Coming soon: .NET 5.0

And, it all changes again! Microsoft recently announced that they'll combine .NET Framework and .NET Core in .NET 5.0. If you want to read more, I wrote in detail why that's another significant and important for .NET developers:

Conclusion

On this post we reviewed a little about the histories of the .NET Framework and .NET Core. We also discussed the main advantages of .NET Core and why you should use it going forward. .NET Core is Microsoft's bold attempt to modernize their omnipresent framework. Some articles to read next are:

About the Author

Bruno Hildenbrand      
Principal Architect, HildenCo Solutions.