.Net 7 New Features Update | RAJESH GAMI




 .NET 7 Preview 1 is now available! .. This is the first preview of the next major .NET release and will bring the next wave of innovation to web development with ASP.NET Core. With

.NET 7, we plan to make a significant investment in ASP.NET Core. Below are some of the areas we would like to focus on.


  • Performance: ..NET 6 includes many performance improvements for ASP.NET Core, and we are working to make ASP.NET Core even faster and more efficient with .NET 7.
  • HTTP/3: HTTP / 3 support is provided as a preview feature in .NET 6. For .NET 7, complete it with the support features enabled by default. In future previews, HTTP / 3 support can be expected to enhance TLS capabilities and other performance improvements.
  • Minimal APIs: Adds endpoint filtering and route grouping support as a core element of the minimal API. It also simplifies the configuration of common API authentication and authorization.
  • gRPC:  We are investing in gRPC JSON transcoding. This feature allows you to use JSON requests and responses to call gRPC services such as the RESTful HTTP API.
  • SignalR: Added support for strongly typed clients and returns results from client calls.
  • Razor: Makes various improvements to the Razor compiler to improve performance and restoring force and enable tool improvements.
  • Blazor Once Blazor hybrid support for .NET MAUI, WPF, and Windows Forms is complete, there will be significant Blazor improvements, including:
    • New .NET Web Assembly features: AOT mixed mode, multithreading, web crypto.
    • Improved support for hot reloads.
    • Improved data binding.
    • More flexible pre-rendering.
    • You can have finer control over the life cycle of the Blazor Server circuit.
    • Improved support for the micro front end.
  • MVCImproved endpoint routing, link generation, and parameter binding.
  • Orleans: ASP.NET Core and the Orleans team are looking for ways to further align and integrate Orleans' distributed programming model with ASP.NET Core. Orleans 4 ships with .NET 7 and focuses on simplicity, maintainability, and performance, including a human-readable stream ID and a new optimized version-tolerant serializer.

For more information on specific ASP.NET Core tasks planned for .NET7, see .NET 7's complete ASP.NET Core roadmap on GitHub.

.NET 7 Preview 1 is the first of many .NET 7 preview releases in preparation for the November 2022 .NET 7 release.

Watch the following video on .net to analyze everything contained in .NET 7 and Asp.net in .NET 7.


Here's an overview of what's new in this preview release: 

  • Minimal API improvements:
    • Support for IFormFile and IFormFileCollection
    • Bind the request body as a Stream or PipeReader 
    • JSON option configuration 
  • SignalR client source generator 
  • Support for nullable models in MVC views and Razor pages 
  • Use JSON property name in a validation error
  • Improved console output for dotnet watch 
  • Configure dotnet watch to reboot whenever there is a significant change 
  • Use Dependency Injection with ValidationAttribute
  • Faster header parsing and writing
  • gRPC JSON transcoding


Get started

Starting to use ASP.NET Core with .NET 7 Preview 1, install the .NET 7 SDK.

If you are using Visual Studio on Windows, we recommend that you install the latest Visual Studio 2022 preview. Support for the .NET 7 preview version of Visual Studio for Mac isn't available yet, but it will be available soon.

To install the latest .NET WebAssembly build tools, run the following command from an elevated command prompt:

dotnet workload install wasm-tools

Upgrade an existing project

To upgrade an existing ASP.NET Core app from .NET 6 to .NET 7 Preview 1:

  • Update the target framework for your app to net7.0.
  • Update all Microsoft.AspNetCore.* package references to 7.0.0-preview.1.*.
  • Update all Microsoft.Extensions.* package references to 7.0.0-preview.1.*.

See also the full list of breaking changes in ASP.NET Core for .NET 7.

Minimal API improvements

IFormFile and IFormFileCollection Support

IFormFile and IFormFileCollection can now be used to handle file uploads with a minimal API.

app.MapPost("/upload", async(IFormFile file) =>
{
    using var stream = System.IO.File.OpenWrite("upload.txt");
    await file.CopyToAsync(stream); 
});
app.MapPost("/upload", async (IFormFileCollection myFiles) => { ... });

Anti-counterfeiting support is required to use this feature for authentication, but it has not yet been implemented. Anti-forgery support for minimal APIs is on our roadmap for .NET 7. Binding to an IFormFile or IFormFileCollection if the request contains an Authorization header, Client Certificate, or Cookie header is currently disabled. This limitation will be fixed when the anti-counterfeiting support work is finished.

Bind the request body as a Stream or PipeReader

Scenarios where the request body is bound as a Stream or PipeReader and the user needs to ingest the data and store it in blob storage or queue the data for later processing in a queue provider (such as Azure Queue) or the cloud. We are now able to support you efficiently. function. The following example shows how to use the new binding.

app.MapPost("v1/feeds", async (QueueClient queueClient, Stream body, CancellationToken cancellationToken) =>
{
    await queueClient.CreateIfNotExistsAsync(cancellationToken: cancellationToken);
    await queueClient.SendMessageAsync(await BinaryData.FromStreamAsync(body), cancellationToken: cancellationToken);
});

Keep the following in mind when using  Stream or PipeReader: 

  • When reading data,  Stream is the same object as HttpRequest.Body
  • By default, the request body is not buffered. Once the text has been read, it cannot be rewound (the stream cannot be read multiple times).
  • Stream / PipeReader cannot be used outside the minimal action handler because the underlying buffer is discarded and/or reused.

JSON option configuration

We are introducing a new and cleaner API, ConfigureRouteHandlerJsonOptions, to configure JSON options for minimal API end-point. This new API avoids confusion with Microsoft.AspNetCore.Mvc.JsonOptions.

var builder = WebApplication.CreateBuilder(args);
builder.Services.ConfigureRouteHandlerJsonOptions(options =>
{
    //Ignore Cycles
    options.SerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles; 
});    

SignalR client source generator

SignalR Client Source Generator generates strongly typed transmit and receive codes based on the interface you define. Instead of the loosely typed.On ("methodName", ...) method, you can reuse the same interface from a strongly typed SignalR hub on the client. Similarly, a hub can implement an interface for that method, and clients can call the hub's methods using the same interface.

To use the SignalR client source generator:

[AttributeUsage(AttributeTargets.Method)]
internal class HubServerProxyAttribute : Attribute
{
}

[AttributeUsage(AttributeTargets.Method)]
internal class HubClientProxyAttribute : Attribute
{
}
  • Add a static partial class to your project and write static partial methods with the [HubClientProxy] and [HubServerProxy] attributes
internal static partial class MyCustomExtensions
{
    [HubClientProxy]
    public static partial IDisposable ClientRegistration<T>(this HubConnection connection, T provider);

    [HubServerProxy]
    public static partial T ServerProxy<T>(this HubConnection connection);
}
  • Use the partial methods from your code!
public interface IServerHub
{
    Task SendMessage(string message);
    Task<int> Echo(int i);
}

public interface IClient
{
    Task ReceiveMessage(string message);
}

public class Client : IClient
{
    // Equivalent to HubConnection.On("ReceiveMessage", (message) => {});
    Task ReceiveMessage(string message)
    {
        return Task.CompletedTask;
    }
}

HubConnection connection = new HubConnectionBuilder().WithUrl("...").Build();
var stronglyTypedConnection = connection.ServerProxy<IServerHub>();
var registrations = connection.ClientRegistration<IClient>(new Client());

await stronglyTypedConnection.SendMessage("Hello world");
var echo = await stronglyTypedConnection.Echo(10);

Support for nullable models in MVC views and Razor Pages

 To improve the experience when using null state checking in ASP.NET Core apps, we have enabled the definition of a nullable page or view model.

@model Product?

Use JSON property names in validation errors

By default, when ModelErrorDictionary is generated by model validation, the property name is used as the error key ("MyClass.PropertyName"). Model property names are usually implementation details and can be difficult to handle for single-page apps. You can now configure validation to use the appropriate JSON property name instead of the new SystemTextJsonValidationMetadataProvider (or NewtonsoftJsonValidationMetadataProvider if you are using Json.NET).

services.AddControllers(options =>
{
    options.ModelMetadataDetailsProviders.Add(new SystemTextJsonValidationMetadataProvider())
});

Improved console output for dotnet watch

We cleaned up the console output from dotnet watch to better align with the log out of ASP.NET Core and to stand out with 😮emojis😍.

Here’s an example of what the new output looks like:

C:BlazorApp> dotnet watch
dotnet watch 🔥 Hot reload enabled. For a list of supported edits, see https://aka.ms/dotnet/hot-reload.
  💡 Press "Ctrl + R" to restart.
dotnet watch 🔧 Building...
  Determining projects to restore...
  All projects are up-to-date for restore.
  You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy
  BlazorApp -> C:UsersdarothDesktopBlazorAppbinDebugnet7.0BlazorApp.dll
dotnet watch 🚀 Started
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:7148
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5041
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:UsersdarothDesktopBlazorApp
dotnet watch ⌚ File changed: .PagesIndex.razor.
dotnet watch 🔥 Hot reload of changes succeeded.
info: Microsoft.Hosting.Lifetime[0]
      Application is shutting down...
dotnet watch 🛑 Shutdown requested. Press Ctrl+C again to force exit.

Configure dotnet watch to always restart for rude edits

Set the DOTNET_WATCH_RESTART_ON_RUDE_EDIT environment variable to true to configure dotnetwatch to always reboot without requesting rude changes (non-reloadable changes).

Inject services into custom validation attributes in Blazor

You can now inject services into custom validation attributes in Blazor. Blazor will be set up  ValidationContext so it can be used as a service provider.

public class SaladChefValidatorAttribute : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var saladChef = validationContext.GetRequiredService<SaladChef>();
        if (saladChef.ThingsYouCanPutInASalad.Contains(value.ToString()))
        {
            return ValidationResult.Success;
        }
        return new ValidationResult("You should not put that in a salad!");
    }
}

// Simple class configured as a service for dependency injection
public class SaladChef
{
    public string[] ThingsYouCanPutInASalad = { "Strawberries", "Pineapple", "Honeydew", "Watermelon", "Grapes" };
}

Faster header parsing and writing

We made several improvements to the performance of header parsing and writing for HTTP/2 and HTTP/3. See the following pull requests for details:

gRPC JSON transcoding

gRPC JSON transcoding allows gRPC services to be used like a RESTful HTTP APIs. Once configured, gRPC JSON transcoding allows you to call gRPC methods with familiar HTTP concepts:

  • HTTP verbs
  • URL parameter binding
  • JSON requests/responses

Of course, gRPC can still be used. RESTful API for gRPC services. There is no duplication! 

ASP.NET Core uses a library called gRPC-HTTP-API to provide experimental support for this feature. For .NET 7, we plan to make this feature a supported part of ASP.NET Core. This feature is not yet included in .NET 7, but you can try out existing experimental packages. For more information, see the gRPC HTTP API getting started documentation.


Thanks 

Rajesh Gami

Full-Stack Developer









No comments:

Post a Comment

RAJESH GAMI - Blog

Digital Signature Pad in Angular | RAJESH GAMI

  What is Signature Pad? Signature Pad could be a JavaScript library for drawing fancy signatures. It supports HTML5 canvas and uses variabl...