dcsimg
March 2, 2021
Hot Topics:

.NET 5: Native Code Improvements and More

  • By Hannes Du Preez
  • Send Email »
  • More Articles »

We covered some of the highlights of Microsoft's .NET 5 release in .NET 5: An Introduction. In this installment we'll concentrate on Native Code improvements mostly, and then move on to Logging and ClickOnce improvements.

Native Code improvements

There are a ton of improvements to working with native code in .NET 5. Herewith is a quick note on most of the changes to native code.

ComWrappers

If you are familiar with COM (Component Object Model) on Windows, you will know that COM is simply a set of binary components that can interact with other components and other applications because their functionalities have been exposed. This is possible via the help of RCW (Runtime Callable Wrappers) and CCW (COM Callable Wrappers).

.NET 5 introduces ComWrappers as a way for third parties to generate custom wrappers. For more on that API, see this GitHub page.

Unmanaged Keyword with Function Pointers

Function pointers in C# 9.0 allow the creation of declarations with an unmanaged calling convention simply by using the unmanaged keyword. Here is a small snippet as an example:

// Platform-dependent default calling convention
delegate* unmanaged<int, int>;

Support for the dynamic keyword in COM objects

The dynamic keyword with COM objects was not really supported in .NET Core 3.x and lower. .NET 5 includes support for the dynamic keyword and COM objects.

SuppressGCTransition

SuppressGCTransitionAttribute circumvents all the safeguards put in place by the runtime around memory management with P/Invokes. This reduces interop overhead and enables high-performance P/Invoke calls in both runtime libraries and third-party libraries.

Logging

The Microsoft.Extensions.Logging library contains many improvements! A custom ConsoleFormatter can be implemented for greater control over formatting and colorization of the console output. In addition to support for custom formatters, Microsoft has also added a built-in JSON formatter for structured JSON logs in the console.

dotnet-runtimeinfo

dotnet-runtimeinfo is a cool new tool in .NET 5 with which you can print environment information. To install it, open the Developer Command Prompt window and enter:

dotnet tool install -g dotnet-runtimeinfo

To run the tool, enter:

dotnet-runtimeinfo

The following figure shows its output on my test environment:


Figure 1 - dotnet-runtimeinfo Output

ClickOnce support

Good news! ClickOnce is now supported in .NET Core 3.1 and .NET 5.0 Windows apps. It has a familiar look and feel in Visual Studio and a modern CI/CD for ClickOnce publishing with command-line flows, with either MSBuild or the Mage tool.

To install the Mage tool, simply enter the following command in the Developer Command Prompt window, as shown in Figure 2 below:

dotnet tool install -g Microsoft.DotNet.Mage

To run Mage, simply enter:

microsoft.dotnet.mage


Figure 2 - Installing the Mage tool

More to come

The list of .NET 5 improvements just keeps growing and growing. In this article I have tried to compile a list of the most invaluable improvements to .NET 5 for seasoned developers. There are many more enhancements and changes to .NET 5, so I will cover more in a follow-up.

 





This article was originally published on January 14, 2021

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.


Thanks for your registration, follow us on our social networks to keep up-to-date