ASP.NET Core Updates - .NET 6 Preview 4 Is Now Available

ASP.NET Microsoft Update

Yesterday was a great day about ASP.NET. .NET 6 Preview 4 update comes and full .NET update is coming a way. Preview 4 update includes many great new improvements to ASP.NET Core.

In this article we going to check a few improvements that preview 4 update includes. E.g. Minimal APIs, APIs perfomance and HTTP Logging. Read the full article on Microsoft developers blog.

What’s New .NET 6 Preview 4 Release

Here is a few improvements what's is included in this update.

  • Introducing minimal APIs
  • Async streaming
  • HTTP logging middleware
  • Use Kestrel for the default launch profile in new projects
  • IConnectionSocketFeature
  • IConnectionSocketFeature
  • .NET Hot Reload updates
  • Generic type constraints in Razor components
  • Blazor error boundaries
  • Blazor WebAssembly ahead-of-time (AOT) compilation
  • .NET MAUI Blazor apps
  • Other performance improvements

Get Start With ASP.NET Core in .NET 6 Preview 4

To get started with ASP.NET Core in .NET 6 Preview 4, you can install the .NET 6 SDK.

Microsoft devblogs recommend installing the latest preview of Visual Studio 2019 16.11 if you using Windows and if you're on macOS install the latest preview of Visual Studio 2019 for Mac 8.10.

Upgrade an existing project

To upgrade an existing ASP.NET Core app from .NET 6 Preview 3 to .NET 6 Preview 4:

  • Update all Microsoft.AspNetCore.* package references to 6.0.0-preview.4.*.
  • Update all Microsoft.Extensions.* package references to 6.0.0-preview.4.*.

See the full list of changes in ASP.NET Core for .NET 6.

Introducing Minimal APIs

.NET 6 introducing minimal APIs for hosting and routing in web applications.

 dotnet new web -o MinApi

Just with a single file and a few small lines of code, you have a fully functioning HTTP API:

ASP.NET 6.0 Minimal APIs

New Routing APIs

New routing APIs allow users to route to any type of method. These methods can use controller-like parameter binding, JSON formatting, and action result execution.

Before:


 app.MapGet("/", async httpContext =>
 {
     await httpContext.Response.WriteAsync("Hello World!");
 });

After (using the new Map overloads):

app.MapGet("/", (Func)(() => "Hello World!"));

Performance

New routing APIs have far less overhead than controller-based APIs. Using the new routing APIs, ASP.NET Core is able to achieve ~800k RPS in the TechEmpower JSON benchmark vs ~500k RPS for MVC.

TechEmpower benchmark

HTTP Logging Middleware

HTTP logging is a new built-in middleware that logs information about HTTP requests and HTTP responses including the headers and entire body.


 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
 {
     app.UseHttpLogging();
 }
 

HTTP logging provides logs of:

  • HTTP Request information
  • Common properties
  • Headers
  • Body
  • HTTP Response information

To configure the HTTP logging middleware, you can specify HttpLoggingOptions in your call to ConfigureServices():


 public void ConfigureServices(IServiceCollection services)
 {
     services.AddHttpLogging(logging =>
     {
         // Customize HTTP logging here.
         logging.LoggingFields = HttpLoggingFields.All;
         logging.RequestHeaders.Add("My-Request-Header");
         logging.ResponseHeaders.Add("My-Response-Header");
         logging.MediaTypeOptions.AddText("application/javascript");
         logging.RequestBodyLogLimit = 4096;
         logging.ResponseBodyLogLimit = 4096;
     });
 }

This results in a new log with the HTTP request information in the Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware category.

Output:


 info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[1]
       Request:
       Protocol: HTTP/1.1
       Method: GET
       Scheme: https
       PathBase:
       Path: /
       QueryString:
       Connection: keep-alive
       Accept: */*
       Accept-Encoding: gzip, deflate, br
       Host: localhost:5001
       User-Agent: PostmanRuntime/7.26.5
       My-Request-Header: blogpost-sample
       Postman-Token: [Redacted]
      

How to use HTTP logging, check HTTP logging docs.


If you like this blog post and it was useful to you, please follow us on Twitter and Facebook.


Sources:

Post a Comment

0 Comments