Showing posts with label DotNet/dotnetCore. Show all posts
Showing posts with label DotNet/dotnetCore. Show all posts

ASP.NET Core and Blazor updates in .NET 7 Preview 6

ASP.NET Core and Blazor updates in .NET 7 Preview 6

 In this article, we going to discuss ASP.NET Core updated in .NET 7 preview 6.

.NET 7 Preview 6 is now available and it includes enhancement or update for ASP.NET Core as well.

Below are some new updates included in .NET 7 preview 6 for asp.net core.

1.      Output caching middleware 

2.      Request decompression middleware

3.      Kestrel support for Web Sockets over HTTP/2

4.      Updates to rate limiting middleware

5.      System.Security.Cryptography support in Web Assembly

6.      Support for logging additional request headers in W3Clogger

7.      MapGroup support for more extension methods

8.      gRPC JSON transcoding multi-segment parameter


To understand in brief the ASP.NET Core work updated for .NET 7 preview 6 go to the ASP.NET Core roadmap for .NET 7.

 Get Start Working with .NET 7 preview 6

If we are looking to work with, ASP.NET Core in .NET 7 Preview 6 then, install the .NET 7 SDK.

If you have Windows then install the latest Visual Studio 2022 preview.

 If you have macOS, then install the latest Visual Studio 2022 for Mac preview.

To install the latest .NET Web Assembly build tools, run the below command from a command prompt:

dotnet workload install wasm-tools

 

 

Upgrading the existing project

If you want to upgrade an existing ASP.NET Core application from .NET 7 Preview 5 to .NET 7 Preview 6 then follow the below commands

  • Update all the Microsoft.AspNetCore.* package references to 7.0.0-preview.6.*.
  • Update all the Microsoft.Extensions.* package references to 7.0.0-preview.6.*.

For a full list of breaking changes in the ASP.NET Core for .NET 7.

1.     Output caching middleware

Output caching is the new middleware in asp.net core for .NET 7 preview 6 which helps us to store results data from our web application and then get data from a caching instead of analyzing them every time, this improves the performance and use resources for other activities.

 

To start working with output caching, need to use the AddOutputCache extension method in IServiceCollection and UseOutputCache extension method in IApplicationBuilder.

 

After doing the above changes then we need to configure the output caching on endpoints like below

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

 

Requests which are coming from the web are sent to “/notcached” check for the current time. The “/cached” endpoint uses the.CacheOutput() extension method, by using it, each request to “/cached” after the first one will get cached response.

 

Even we can customize the output caching by using VaryByQuery.

 

We can control the caching using VaryByQuery by passing parameters as below.

// Cached entries will vary by culture, but any other additional query
// is ignored and returns the same cached content.
app.MapGet("/query", () => DateTime.Now.ToString()).CacheOutput(p => p.VaryByQuery("culture"));

 

2.     Request decompression middleware

·         The request decompression middleware is the new middleware in asp.net core for .NET 7 preview 6 for using the Content-Encoding HTTP header to automatically identify and for decompressing requests using compressed content so that developers of the server do not need to handle it.

·         The request decompression middleware is added by using the UseRequestDecompression extension method in IApplicationBuilder and the AddRequestDecompression extension method in IServiceCollection.

 

3. Kestrel support for WebSockets over HTTP/2

WebSockets was originally designed for HTTP/1.1 and now it upgraded to HTTP/2. Using WebSockets on HTTP/2 has some new features like multiplexing and header compression that help us to reduce the time and the resources which need when we are making multiple requests to the server.

 It supports now also available in the Kestrel on all HTTP/2-enabled platforms.

4. Updates to rate limiting middleware

The rate-limiting middleware supports rate limiting on the specific endpoints, which can be combined with the global limiter that runs in all requests. Below is the example which adds a TokenBucketRateLimiter to the endpoint, and uses the global ConcurrencyLimiter:

var endpointName = "dotnetofficeEndpoint";
 
// Defining the endpoint limiters and the global limiter.
// dotnetofficeEndPoint will be limited by the TokenBucket Limiter with the max permit count of 6 and the queue depth of 3.
var options = new RateLimiterOptions()
    .AddTokenBucketLimiter(endpointName, new TokenBucketRateLimiterOptions(3, QueueProcessingOrder.OldestFirst, 6, TimeSpan.FromSeconds(10), 1))
 
// The global limiter will be the concurrency limiter with the max permit count of 10 and the queue depth of 5.
options.GlobalLimiter = PartitionedRateLimiter.Create<HttpContext, string>(context =>
    {
        return RateLimitPartition.CreateConcurrencyLimiter<string>("globalDotnetofficeLimiter", key => new ConcurrencyLimiterOptions(10, QueueProcessingOrder.NewestFirst, 5));
    });
app.UseRateLimiter(options);
 
app.MapGet("/", () => "dot net office!").RequireRateLimiting(endpointName);

 

It also supports adding the custom rate limiting by using the AddPolicy method in RateLimiterOptions. 

5. System.Security.Cryptography support in WebAssembly

 .NET 6 supported the SHA family for hashing algorithms when we are running in WebAssembly. but .NET 7 allows more cryptographic algorithms by using  SubtleCrypto when possible.

5. Support for logging additional request headers in W3CLogger

We can now specify additional request headers to log while using the W3C logger by calling AdditionalRequestHeaders() method in W3CLoggerOptions.

services.AddW3CLogging(logging =>
{
    logging.AdditionalRequestHeaders.Add("https://www.dotnetoffice.com ");
    logging.AdditionalRequestHeaders.Add("client logger");
});

 

 

6. MapGroup support for more extension methods

Route groups allow us to define the groups for an endpoint using the common route prefix and the common set of conventions.

These conventions contains the extension method for example RequireAuthorization()RequireCors() and their associated attributes. 

Previously Methods like ExcludeFromDescription(),WithTags()WithDescription() and the WithSummary(),  uses the  RouteHandlerBuilder instead of IEndpointConventionBuilder interface and making them incompatible with RouteGroupBuilder returned by MapGroup().

Starting with Preview 6, all extension methods now use the  IEndpointConventionBuilder interface for making them compatible with route groups.

7. gRPC JSON transcoding multi-segment parameters

gRPC JSON transcoding is the new feature in the .NET 7 for turning gRPC APIs into RESTful APIs.

now Preview 6 is support multi-segment parameters in the gRPC JSON transcoding routes. This feature brings the ASP.NET Core transcoding into parity with the other transcoding solutions in gRPC ecosystem.

It is now possible to configure the gRPC APIs for binding properties with multi-segment parameters.

Now we talk about the .NET 7 preview 6 for blazor perceptive then there are a couple of upgrades happen, so let's start

1.      Empty Blazor project templates

2.      QuickGrid component for Blazor

3.      Blazor custom elements are no longer experimental

 

Ø  Empty Blazor project templates

When we work with .NET 6 and select the blazor project either the server project or the web assembly project, both contain the default application, but with the .NET 7 preview 6 now we can create both server and web assembly empty project as well.

Now both “Blazor Server App Empty” and the “Blazor Web Assembly App Empty” project templates are now just like their non-empty counterparts but without having any demo code.

 

Ø  QuickGrid component for Blazor

Now in the .NET, 7 previews 6 QuickGrid components are added newly for displaying the data in the grid. QuickGrid components help us in sorting, filtering, and pagination of the data.

To start working with QuickGrid:

  1. Add the reference to Microsoft.AspNetCore.Components.QuickGrid package.
  2. Add the below Razor code to display a grid.
<QuickGrid Items="@employee ">
   <PropertyColumn Property="@(p => p. employeeId)" Sortable="true" />
    <PropertyColumn Property="@(p => p.Name)" Sortable="true" />
    <PropertyColumn Property="@(p => p.BirthDate)" Format="yyyy-MM-dd" Sortable="true" />
</QuickGrid>

 

@code {
    record Employee(int employeeId, string Name, DateOnly BirthDate);
 
    IQueryable<Employee> employee = new[]
    {
        new Person(1, "dotnetoffice", new DateOnly(2018, 1, 1)),
        new Person(2, "ABC", new DateOnly(2002, 1, 1)),
        new Person(3, "XYZ", new DateOnly(1990, 1, 2))        
    }.AsQueryable();
}

 

 

For more follow  QuickGrid for Blazor demo site

Ø  Blazor custom elements no longer experimental

Previous blazor package like Microsoft.AspNetCore.Components.CustomElements and standards based custom elements are no longer part of.NET 7 release.

To create a custom element in Blazor,we have to register a Blazor root component as the custom element like below

options.RootComponents.RegisterCustomElement<Counter>("my-Custom_element");

 

 

Then we can use this custom element with any other web framework.

<my-Custom_element increment-amount="10"></my- Custom_element>

 

 

So these are above a couple of the upgrades in .NET7 preview 6 versions with respect to ASP.NET core and Blazor.

For more follow the link


------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------