ASP.NET Core Furthers Minimal API Work in .NET 7 Preview 3

One of the highlights of improved ASP.NET Core functionality in .NET 6 was support of minimal APIs, for which work is still ongoing in the new .NET 7 Preview 3.

Minimal APIs are architected to create HTTP APIs with minimal dependencies, says Microsoft, which notes they are ideal for microservices and ASP.NET Core apps in which developers want to include only the minimum files, features and dependencies.

In .NET 7 Preview 3 there is new support for route handler filters in minimal APIs, along with improved unit testability for minimal route handlers.

"In this preview, we introduce support for filters in route handlers in minimal applications," said Daniel Roth, principal program manager, in an April 12 blog post. "Filters are executed before the core route handler logic and can be used to inspect and modify handler parameters or intercept handler execution.

"Filters can be registered onto a handler using a variety of strategies. For example, you can register a filter using a RouteHandlerFilterDelegate and the AddFilter extension method."

As far as improved unit testability for minimal route handlers, Roth said IResult implementation types are now publicly available in the namespace Microsoft.AspNetCore.Http with the suffix HttpResult (OkObjectHttpResult, ProblemHttpResult and so on). He said this helps developers more easily unit test minimal route handlers when using named methods instead of lambdas.

Minimal API Work in .NET 7
[Click on image for larger view.] Minimal API Work in .NET 7 (source: Microsoft).

The ASP.NET Core Roadmap for .NET 7 provides more information on what work remains, including for minimal APIs (shown above).

Other highlights of the preview as called out by Roth include:

  • Developer exception page dark mode: Developers can now enjoy a new dark mode when reading about unhandled exceptions:
    Developer Exception Page Dark Mode
    [Click on image for larger view.] Developer Exception Page Dark Mode (source: Microsoft).
  • Bind using TryParse in MVC and API controllers: Developers can now bind controller action parameter values using a TryParse method that has one of two signatures.
  • New Results.Stream() overloads: These are for scenarios where developers need access to the underlying HTTP response stream without buffering. "These overloads also improve cases where your API wants to stream data to the HTTP response stream, like from Azure Blob Storage."
  • Improved HTTP/2 performance when using many streams on a connection: "We made a change in our HTTP/2 frame writing code that improves performance when there are multiple streams trying to write data on a single HTTP/2 connection. We now dispatch TLS work to the thread pool and more quickly release a write lock that other streams can acquire to write their data. The reduction in wait times can yield significant performance improvements in cases where there is contention for this write lock."
  • New ServerReady event for measuring startup time: Coders using EventSource for metrics/diagnostics who want to measure ASP.NET Core app startup times can now use the new ServerReady event in the Microsoft.AspNetCore.Hosting source, which represents the point where the server is up and running.

About the Author

David Ramel is an editor and writer for Converge360.

comments powered by Disqus


Subscribe on YouTube