What's coming in .NET Core 2.1

The preview version of .NET Core 2.1 is launched on February 27th 2018. We can start developing a .NET Core 2.1 application using Visual Studio 2017 15.6 Preview 6 or later, and also using Visual Studio Code. Let's see what is newly added to .NET Core 2.1.

  • Build performance: In .NET Core 2.1, build time performance has improved. CLI tools and MSBuild have improved and are much faster than before.
  • Minor version roll forward: We can run the .NET Core X.x application on later minor versions with the same major version range, such as .NET Core 2.1 applications on .NET Core 2.6. This roll forward feature is applicable to minor versions only, so 2.1 can't be automatically rolled forward to .NET Core 3.0, or any other major version. Roll forward behavior is only relevant when the expected .NET Core version is not present in the given environment. We can disable this feature using:
    • Environment variable: DOTNET_ROLL_FORWARD_ON_NO_CANDIDATE_FX=0
    • runtimeconfig.json: rollForwardOnNoCandidateFx=0
    • CLI: roll-forward-on-no-candidate-fx=0
  • Sockets performance and HTTP managed handler: As part of the new version, socket performance has increased. Sockets are the basis of outgoing and incoming network communication. In .NET Core 2.0 ASP.NET, the Kestrel web server and HttpClient use native Socket not the .NET Socket class. There will be three significant performance improvements for sockets. It supports Span<T> and Memory<T> in Socket and NetworkStream. SocketHttpHandler performance has improved. A few benefits are:
    • Platform dependencies have been eliminated on libcurl (linux) and WinHTTP (Windows)—this simplifies both development, deployment, and servicing
    • Consistent behavior across all platforms and platform/dependency versions
    • We can opt in to using the SocketHTTPHandler in one of the following ways with Preview 1:
      • Environment variable: COMPlus_UseManagedHttpClientHandler=true
      • AppContext: System.Net.Http.UseManagedHttpClientHandler=true
  • Span<T>, Memory<T>: New types are introduced for using arrays and for other types of memory, which is efficient and increases performance. Using Span, we can pass a subset of an array, for example 5 elements of a 100 element array, we can create a Span<T> which provides a virtual of that array, without time or space cost. Now, no need to make a copy of those five arrays. This is also struct, so no allocation cost. With slicing capabilities, it obviates the need for expensive copying and allocation in many cases, such as string manipulation buffer management and so on. Here is an example of creating Span<T> from an array:
var arrayExample= new byte[10];
Span<byte> bytes = arrayExample; // Implicit cast from T[] to Span<T>

From there, we can easily and efficiently create a Span to represent/point to just a subset of this array, utilizing an overload of the span’s Slice method. From there, you can index into the resulting span to write and read data in the relevant portion of the original array:

Span<byte> slicedBytes = bytes.Slice(start: 5, length: 2);
slicedBytes[0] = 42;
slicedBytes[1] = 43;
Assert.Equal(42, slicedBytes[0]);
Assert.Equal(43, slicedBytes[1]);
Assert.Equal(arrayExample [5], slicedBytes[0]);
Assert.Equal(arrayExample [6], slicedBytes[1]);
slicedBytes[2] = 44; // Throws IndexOutOfRangeException
bytes[2] = 45; // OK
Assert.Equal(arrayExample [2], bytes[2]);
Assert.Equal(45, arr[2]);
  • Windows Compatibility Pack: When we port existing code from the .NET Framework to .NET Core, we can use the new Windows Compatibility Pack. It provides access additional 20,000 APIs, compared to what is available in .NET Core. This includes System.Drawing, EventLog, WMI, Performance Counters, and Windows Services. The following example illustrates accessing the Window registry with APIs provided by the Windows Compatibility Pack. The sample fetches the value of TechnicalEditor of book NetCore2ByExample from CurrentUser registry hive:
using (var userHiveRegKey = Registry.CurrentUser.OpenSubKey(@"SoftwarePacktBooksNetCore2ByExample"))
{
var value = userHiveRegKey?.GetValue("TechnicalEditor");
}

To learn more about .NET Core 2.1 features, please visit the following resources:

With this, we conclude the chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset