.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.
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.
what's new in this preview release:?
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:
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:?
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
{
}
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);
}
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: https://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:
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
Graphic Designer & Software Developer
2 年https://www.dhirubhai.net/posts/sudeb-mazumder-1910231b8_sudeb420-i-will-do-professional-awesome-activity-6952594733164621824-R0LM?utm_source=linkedin_share&utm_medium=android_app