.NET 7 Preview 6: This is news for web developers

.NET 7 Preview 6: This is news for web developers

For Blazor, .NET 7.0 Preview 6, released on July 12, 2022, delivers two new project templates: “Blazor Server App Empty” and “Blazor WebAssembly App Empty”. Unlike the Blazor project templates available since ASP.NET Core 3.x, the new “Empty” templates don’t include multiple webpages and Bootstrap as the CSS framework. Instead, developers get a single web page with the following content:

@page "/"
<h1>Hello, world!</h1>

The only supplied CSS file /wwwroot/site.css contains only the necessary CSS for the information box when a runtime error occurs. The new templates, which are created via the Preview 3.0 of Visual Studio 2022 Version 17.3 that was released at the same time or via the command line, do without a master page or navigation bar:

dotnet new blazorserver-empty
dotnet new blazorwasm-empty

For the first time, Microsoft is delivering a data grid control for Blazor – previously Blazor developers had to switch to third-party or community solutions. With the name “QuickGrid”, Microsoft apparently wants to emphasize that the control is intended to provide a performance basis for creating Blazor DataGrid components.

The new control is in the Microsoft.AspNetCore.Components.QuickGrid NuGet package, but is experimental for now. A live demonstration using Blazor WebAssembly is available on GitHub in the QuickGridSamples. So far, however, QuickGrid offers neither data processing nor nesting of rows. It is also not possible for the user to rearrange the columns afterwards.

The Microsoft.AspNetCore.Components.CustomElements NuGet package for creating HTML Custom Elements (Web Components) is now an official part of .NET 7.0 as of Preview 6. The package has been around since .NET 6.0 RC1, but until now it has had experimental status. The associated documentation also contains a warning, common for experimental features: “Experimental features are provided for the purpose of exploring feature viability and may not ship in a stable version.”

Another innovation in Blazor is the support of various SHA algorithms (SHA1, SHA256, SHA364, SHA512, each including an HMAC variant) in Blazor WebAssembly directly via the SubtleCrypto interface of the Web Crypto API in modern browsers. Previously, the SHA algorithms in Blazor WebAssembly were only accessible via the .NET implementation, which is now available as a fallback if SubtleCrypto is unavailable. By the time .NET 7 is released, Microsoft plans to also provide the AES-CBC, PBKDF2 and HKDF algorithms directly.

In ASP.NET Core 7.0 Preview 6, Microsoft added two new middleware components. The new request decompression middleware handles content-encoded HTTP requests inside ASP.NET Core, so the web server doesn’t have to worry about it anymore. Developers can do this in ASP.NET Core by calling AddRequestDecompression() and UseRequestDecompression() activate in the start phase. Supported compression methods are Brotli, Deflate and GZip. Other methods can be used with the interface IDecompressionProvider implement yourself.

The output caching middleware, which is also new, can be activated with AddOutputCache() and UseOutputCache()stores the result of an HTTP request in memory if the method CacheOutput() is used:

app.MapGet("/notcached", () => DateTime.Now.ToString());
app.MapGet("/cached", () => DateTime.Now.ToString()).CacheOutput();

Caching depending on a URL parameter, header or other values ​​is possible via VaryByQuery(), [codeVaryByHeader()[/code] or VaryByValue()as the following example shows:

app.MapGet("/cached", () => DateTime.Now.ToString()).CacheOutput(p => p.VaryByQuery("culture"));

The platform-neutral web server Kestrel that comes with ASP.NET Core now also supports WebSockets over HTTP/2 instead of just over HTTP/1.1. ASP.NET Core SignalR also offers WebSockets over HTTP/2 in the preview. The HTTP version to be used is negotiated automatically. It should be noted that Firefox currently also has the flag network.http.spdy.websockets is to be set, while this is already activated in Chrome and Edge in standard mode.

Microsoft has – as decided according to GitHub on April 13, 2022 – the C# language feature “Parameter Null Cheching” with double exclamation mark introduced in earlier previews void Print(string s!!) { … } expanded again from the C# 11 compiler. Now in .NET 7.0 Preview 6 it comes up when running !! to the compiler error “The ‘parameter null-checking’ feature is not supported”. Microsoft decided not to support the null checking parameter.

The .NET base class library in .NET 7.0 Preview 6 provides type converters for the System.DateOnly and System.TimeOnly base data types introduced in .NET 6.0. An example is as follows:

TypeConverter dateOnlyConverter = TypeDescriptor.GetConverter(typeof(DateOnly));
DateOnly? date = dateOnlyConverter.ConvertFromString("2022-07-13") as DateOnly?;

According to the documentation, the 128-bit long integer types System.Int128 and System.UInt128 have been available since .NET 7.0 Preview 5, which according to the blog entry are now also equipped with suitable type converters in Preview 6. However, the compiler in .NET 7.0 Preview 6 does not yet find these two 128-bit data types.

Unfortunately, this is not the first time that Microsoft has praised features that have not yet been implemented (see also the Heise report on Preview 4). The Microseconds and Nanoseconds properties advertised in the Preview 4 blog entry are still not available in Preview 6 either.

In addition, there is a regression problem in .NET 7 Preview 6 that is not included in the Known Issues: The source code generator for regular expressions (RegexGenerator) introduced in .NET 7 Preview 2 now generates program code that cannot be compiled (see Fig. 1 ).

The one by annotation [RegexGenerator] Compiler generated program code does not compile in .NET 7.0 Preview 6 (Fig. 1)

The JSON serializer System.Text.Json In .NET 7 Preview 6, Microsoft added adaptability to the serialization via so-called Type Info Resolvers. This allows developers to customize serialization and deserialization without having to change the type being processed. The program code in Listing 1 ensures that the “Memo” property of the “Point” class is ignored during serialization and deserialization and that the coordination X and Y can also be deserialized from character strings such as “123” and “456”. The following listing illustrates the customization of serialization and deserialization:

using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using ITVisions;
 
namespace NET7Console;
 
public class Punkt
{
 public int X { get; set; }
 public int Y { get; set; }
 public string Memo { get; set; } = "?";
}
 
 
internal class FCL_JSON
{
 public static void Run()
 {

  JsonSerializerOptions options = new()
  {
   TypeInfoResolver = new DefaultJsonTypeInfoResolver()
   {
    Modifiers =
        {
            (JsonTypeInfo jsonTypeInfo) =>
            {
 
             // Ignoriere alle Properties, die mit "Memo" beginnen
            var memoProp = jsonTypeInfo.Properties.FirstOrDefault(p=>p.Name.StartsWith("Memo"));
            if (memoProp != null)
             {
              jsonTypeInfo.Properties.Remove(memoProp);
             }
            
 
            // Erlaube Zahlen aus Zeichenketten zu deserialisieren
            if (jsonTypeInfo.Type == typeof(int))
            {
                    jsonTypeInfo.NumberHandling = JsonNumberHandling.AllowReadingFromString;
 
            }
            }
 
        }
   }
  };
 
  Punkt point = JsonSerializer.Deserialize<Punkt>(@"{""X"":""123"",""Y"":""456"",""Memo"":""Testposition""}", options);
  Console.WriteLine($"({point.X},{point.Y},{point.Memo})"); // (123,456,?)
 
 }
 
}

Authors of .NET project templates can now use “Constraints” in the template.json file to specify requirements for using the templates, including dependencies on the operating system, template host (CLI or Visual Studio or Visual Studio for Mac), on the respective .NET SDK version and the installed .NET SDK workloads. The following code listing serves for clarification:

"constraints": {
    "windows-only": { // Custom name - not validate
      "type": "os", // Type of the constraint - used to match to proper Constraint component to evaluate the constraint
      "args": "Windows" // Arguments passed to the evaluating constraint component
    },
   "7-Preview": {
           "type": "sdk-version",
           "args": "7.*.*-*"
       }
  }

So far, however, the constraints have only been checked in the .NET SDK CLI, not in Visual Studio. In addition, there are now multi-choice parameters when creating projects and improved error messages.

In version .NET 7.0 Preview 6 (exact version number on NuGet.org: 7.0.0-preview.6.22329.4), Microsoft has again dealt with performance optimization in the object-relational mapper Entity Framework Core. Three points are optimized:

  1. Waiver of a transaction if SaveChanges() only sends a single SQL command.
  2. Faster insertion of multiple records in SQL Server (see table).
  3. Merge commands when inserting dependent records
Insert_four_rows 6.0 Remote 12.93ms 0.258ms 0.651ms
Insert_four_rows 7.0 Remote 4.99ms 0.098ms 0.198ms

Table 1: Performance increase when inserting multiple records in SQL Server in Entity Framework Core 7.0 Preview 6 (Source: Microsoft)

The final version of .NET 7.0 is announced for November 8, 2022. This version will be supported until May 2024. Heise and the .NET experts from IT-Visions will present .NET 7 in a one-day online event on November 22, 2022.

Hunter Biden's iCloud account hacked Previous post Hunter Biden’s iCloud account hacked
c't workshop: Introduction to GitLab |  hot online Next post c’t workshop: Introduction to GitLab | hot online