Exploring What .NET 8 Preview Brings for .NET Core
Table of Contents

Exploring What .NET 8 Preview Brings for .NET Core

Introduction

In our previous post, we introduced the exciting new features in .NET 8 Preview, highlighting the various improvements and enhancements it brings to the .NET ecosystem. With the recent availability of .NET 8 Preview 4, we're thrilled to dive deeper into the significant updates it offers to ASP.NET Core. This release includes a host of advancements in Blazor, API authoring, Native AOT support, and authentication and authorization. In this blog post, we'll guide you through these new features, providing insights and examples to help you take advantage of everything .NET 8 Preview 4 has to offer in your ASP.NET Core projects. So, let's get started!

Blazor Updates

As one of the fastest-growing modern frameworks, Blazor has undoubtedly made an impact in the world of web development. With the .NET 8 Preview 4 release, we can expect generous enhancements that aim to improve user experiences and broaden development possibilities.

Streaming Rendering with Blazor Components

Server-side rendering (SSR) with Blazor components now allows streaming content updates through the response stream. This feature significantly improves the user experience. To implement streaming rendering with Blazor components, follow these steps:  

  • Begin by adding the new Blazor script using <script src="_framework/blazor.web.js" suppress-error="BL9992"></script>
  • To activate streaming rendering for a particular component, use the [StreamRendering(true)] attribute.

Streaming is a feature that the whole web development community started to be interested in thanks to React, and its version 18. We wrote more about it here.

Handling Form Posts with Blazor SSR

One of the new features in Blazor SSR is the ability to handle form posts in a more streamlined manner. This improvement simplifies the process of creating web forms and processing their submissions. Here's how you can take advantage of this functionality in Blazor:

  • Set up a model binding context using the `CascadingModelBinder` component. You can do this easily by incorporating it into your app's main layout:
<CascadingModelBinder>   @Body </CascadingModelBinder> 
  • Define a form in Blazor using the existing EditForm component and corresponding input components like InputText, InputSelect, etc. The EditForm component will render a standard HTML form element, allowing you to use the `method` attribute to specify how the form should send a POST request. Note that EditForm event handlers do not support GET requests.
  • As support for model binding and validating request data has not yet been implemented, you can manually handle the request data using the FormDataProvider service. This service provides access to form data and specifies the intended form handler.

The updated Blazor SSR form handling will enable developers to create more dynamic and interactive web forms, elevating the user experience.

Route to Named Elements in Blazor

In .NET 8 Preview 4, Blazor introduces support for client-side routing that allows navigation to specific HTML elements on a page using standard URL fragments. This means that if you assign an identifier to an HTML element using the `id` attribute, Blazor will automatically scroll to that element when the URL fragment matches the element identifier. This feature offers a seamless and direct way to navigate through various sections of a webpage.

Webcil Packaging for Blazor WebAssembly Apps

With the introduction of Webcil in .NET 8 Preview 4, developers can now benefit from a web-friendly packaging of .NET assemblies that removes content specific to native Windows execution. This eliminates issues related to deploying environments that block .dll file downloads or use.

To enable the usage of Webcil for your Blazor WebAssembly apps, simply add the WasmEnableWebcil property to your project file:

<PropertyGroup>   <WasmEnableWebcil>true</WasmEnableWebcil> </PropertyGroup> 

If you encounter any issues while using .webcil files in your environment, please report them by creating an issue on GitHub.

API Authoring

API authoring capabilities have been expanded, allowing for a more comprehensive and efficient development process.

Expanded Support for Form Binding in Minimal APIs  

This release brings an extensive range of support for form binding in minimal APIs. Form-based parameters are now inferred without the need for the FromForm attribute. This advanced support includes IFormCollection, IFormFile, IFormFileCollection, and OpenAPI metadata. By leveraging these improvements, developers can create more robust and flexible APIs that cater to various scenarios and requirements.

API Project Template Includes .http File

The API project template now comes with an .http file, allowing developers to send requests to defined endpoints within the application directly from the new HTTP editor in Visual Studio. This streamlined approach will enable developers to quickly test and iterate their APIs, ensuring a more efficient development process.

With these API authoring updates, developers can expect a more seamless and powerful experience when working with ASP.NET Core in .NET 8 Preview 4.

Native AOT

.NET 8 Preview 4 also brings advancements to Native AOT, offering better performance, configurability, and support for various app features.

Logging and Exception Handling in Compile-Time Generated Minimal APIs

Request Delegate Generator (RDG) is introduced in .NET 8 Preview 4 to improve the handling of parameter binding failures, logging, and exception handling for compile-time generated minimal APIs. With RDG, developers can quickly identify and respond to bad HTTP request exceptions, ensuring a smoother user experience.  

ASP.NET Core Top-Level APIs Annotated for Trim Warnings

To help developers identify features that are incompatible with Native AOT, the main entry points to subsystems have been annotated. This allows developers to see warnings when calling methods that are not trim-safe, ensuring they understand issues that may arise when working with Native AOT.

Reduced App Size with Configurable HTTPS Support

With the updated WebApplication.CreateSlimBuilder() in .NET 8 Preview 4, configurable HTTPS support allows for a reduced app size that excludes default HTTPS or HTTP/3 support. This improvement is beneficial for apps running behind a TLS termination proxy, like those hosted on Azure. The lighter app size results in faster load times and better overall performance.

Worker Service Template Updates

Worker Service templates in .NET 8 Preview 4 now include support for the --aot flag, enabling developers to create worker service projects with AOT publishing enabled. This update simplifies the configuration process and optimizes the published app's performance.

Additional Default Services Configured in Slim Builder

The WebApplication.CreateSlimBuilder() API now includes additional default services to improve the development experience while maintaining a total app size below 10 MB. These default services include JSON file configuration, user secrets configuration, console logging, and logging configuration.  

API Template JSON Configuration Changes

The .NET 8 Preview 4 release has updated JsonSerializerOptions.TypeInfoResolverChain API, providing developers with more control over JSON configuration in their API templates.

Support for JSON Serialization of Compiler-Generated IAsyncEnumerable<T> Unspeakable Types

The .NET 8 Preview 4 release introduces support for JSON serialization of IAsyncEnumerable<T> implementations implemented by the C# compiler. This enables the use of IAsyncEnumerable<T> in ASP.NET Core projects configured to publish native AOT, which is particularly useful when working with asynchronously returned enumerations.

With these advancements in Native AOT, developers can expect better performance, configurability, and support for their applications in .NET 8 Preview 4.

Authentication and Authorization

This release also brings enhancements to authentication and authorization within ASP.NET Core, improving the overall security and customization of your applications.

Identity API Endpoints

The new MapIdentityApi<TUser>() extension method introduces two new API endpoints: /register and /login. These endpoints aim to simplify the process of integrating ASP.NET Core Identity into JavaScript-based single-page apps (SPA) or Blazor apps. The Identity API endpoints provide a more suitable solution for SPA and mobile applications by replacing the default ASP.NET Core Identity UI with JSON API endpoints.  

Improved Support for Custom Authorization Policies with IAuthorizationRequirementData

Custom authorization policies have also been improved in .NET 8 Preview 4. By leveraging the IAuthorizationRequirementData interface, developers can implement custom authorization policies with fewer lines of code and more flexibility. This improvement allows for better customization and enhanced security in your applications.

In conclusion, the .NET 8 Preview 4 release brings a plethora of enhancements to the ASP.NET Core ecosystem, equipping developers with sophisticated tools and features to build and deploy high-performance web applications more efficiently. With continued updates and improvements, ASP.NET Core in .NET 8 is poised to become an essential framework for modern web development.

ASP.NET Core Metrics

One of the most valuable additions to .NET 8 Preview 4 is the implementation of ASP.NET Core metrics. Metrics are essential for monitoring the health of your application and generating alerts. With the addition of System.Diagnostics.Metrics throughout ASP.NET Core hosting, Kestrel, and SignalR, developers now gain access to a powerful set of tools that help ensure their applications run smoothly and efficiently.

As you can’t manage if you can’t track/measure, these improvements are more important than one would think.

The Metrics API offers several improvements over existing event counters:  

New Measurement Types

Metrics introduces new measurement types, such as counters, gauges, and histograms, giving developers a more comprehensive set of data points to monitor and track their applications' performance.  

Powerful Reporting with Multi-Dimensional Values

Metrics support multi-dimensional values, allowing for more nuanced and detailed reporting. This powerful feature enables developers to gain deeper insights into their applications' behavior and troubleshoot issues more effectively.  

Integration into the Cloud-Native Ecosystem

By aligning with OpenTelemetry standards, Metrics ensures seamless integration into the broader cloud-native ecosystem. This compatibility makes it easier for developers to adopt and leverage Metrics in their ASP.NET Core projects.

With the implementation of ASP.NET Core Metrics in .NET 8 Preview 4, developers can expect a more streamlined and efficient approach to monitoring application health and generating alerts. The enhanced metrics capabilities will enable developers to create and maintain high-quality, performant applications with greater ease.

Conclusion

The .NET 8 Preview 4 release significantly elevates the capabilities of ASP.NET Core, providing developers with a powerful framework and cutting-edge features to create high-performance web applications. With updates to Blazor, enhancements to API authoring, advancements in Native AOT support, improved authentication and authorization, and the introduction of ASP.NET Core Metrics, developers can expect a more streamlined and efficient development experience.

As the ASP.NET Core ecosystem continues to innovate and evolve, it's crucial for developers to stay up-to-date with the latest advancements and best practices. The rich offering of features in .NET 8 Preview 4 showcases the commitment to delivering an essential framework for modern web development. Embrace these updates, explore the new possibilities, and stay tuned for future releases as ASP.NET Core paves the way for the next generation of web applications.

Liked the article? subscribe to updates!
360° IT Check is a weekly publication where we bring you the latest and greatest in the world of tech. We cover topics like emerging technologies & frameworks, news about innovative startups, and other topics which affect the world of tech directly or indirectly.

Like what you’re reading? Make sure to subscribe to our weekly newsletter!
Relevant Expertise:
No items found.
Share

Join 17,850 tech enthusiasts for your weekly dose of tech news

By filling in the above fields and clicking “Subscribe”, you agree to the processing by ITMAGINATION of your personal data contained in the above form for the purposes of sending you messages in the form of newsletter subscription, in accordance with our Privacy Policy.
Thank you! Your submission has been received!
We will send you at most one email per week with our latest tech news and insights.

In the meantime, feel free to explore this page or our Resources page for eBooks, technical guides, GitHub Demos, and more!
Oops! Something went wrong while submitting the form.

Related articles

Our Partners & Certifications
Microsoft Gold Partner Certification 2021 for ITMAGINATION
ISO-IEC 27001:2013 ITMAGINATION ISO 9001 ITMAGINATION
© 2024 ITMAGINATION, A Virtusa Company. All Rights Reserved.