{"id":39401,"date":"2022-04-12T08:32:47","date_gmt":"2022-04-12T15:32:47","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/dotnet\/?p=39401"},"modified":"2022-04-19T11:22:26","modified_gmt":"2022-04-19T18:22:26","slug":"asp-net-core-updates-in-dotnet-7-preview-3","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/asp-net-core-updates-in-dotnet-7-preview-3\/","title":{"rendered":"ASP.NET Core updates in .NET 7 Preview 3"},"content":{"rendered":"<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/announcing-dotnet-7-preview-3\">.NET 7 Preview 3 is now available<\/a> and includes many great new improvements to ASP.NET Core.<\/p>\n<p>Here&#8217;s a summary of what&#8217;s new in this preview release:<\/p>\n<ul>\n<li>Support for route handler filters in minimal APIs<\/li>\n<li>Improved unit testability for minimal route handlers<\/li>\n<li>Bind using <code>TryParse<\/code> in MVC and API controllers<\/li>\n<li>New <code>Results.Stream()<\/code> overloads<\/li>\n<li>Improved HTTP\/2 performance when using many streams on a connection<\/li>\n<li>New <code>ServerReady<\/code> event for measuring startup time<\/li>\n<li>Developer exception page dark mode<\/li>\n<\/ul>\n<p>For more details on the ASP.NET Core work planned for .NET 7 see the full <a href=\"https:\/\/aka.ms\/aspnet\/roadmap\">ASP.NET Core roadmap for .NET 7<\/a> on GitHub.<\/p>\n<h2>Get started<\/h2>\n<p>To get started with ASP.NET Core in .NET 7 Preview 3, <a href=\"https:\/\/dotnet.microsoft.com\/download\/dotnet\/7.0\">install the .NET 7 SDK<\/a>.<\/p>\n<p>If you&#8217;re on Windows using Visual Studio, we recommend installing the latest <a href=\"https:\/\/visualstudio.com\/preview\">Visual Studio 2022 preview<\/a>. Visual Studio for Mac support for .NET 7 previews isn&#8217;t available yet but is coming soon.<\/p>\n<p>To install the latest .NET WebAssembly build tools, run the following command from an elevated command prompt:<\/p>\n<pre><code class=\"language-console\">dotnet workload install wasm-tools<\/code><\/pre>\n<blockquote>\n<p>Note: Building .NET 6 Blazor projects with the .NET 7 SDK and the .NET 7 WebAssembly build tools is currently not supported. This will be addressed in a future .NET 7 update: <a href=\"https:\/\/github.com\/dotnet\/runtime\/issues\/65211\">dotnet\/runtime#65211<\/a>.<\/p>\n<\/blockquote>\n<h2>Upgrade an existing project<\/h2>\n<p>To upgrade an existing ASP.NET Core app from .NET 7 Preview 2 to .NET 7 Preview 3:<\/p>\n<ul>\n<li>Update all Microsoft.AspNetCore.* package references to <code>7.0.0-preview.3.*<\/code>.<\/li>\n<li>Update all Microsoft.Extensions.* package references to <code>7.0.0-preview.3.*<\/code>.<\/li>\n<\/ul>\n<p>See also the full list of <a href=\"https:\/\/docs.microsoft.com\/dotnet\/core\/compatibility\/7.0#aspnet-core\">breaking changes<\/a> in ASP.NET Core for .NET 7.<\/p>\n<h2>Support for route handler filters in minimal APIs<\/h2>\n<p>In this preview, we introduce support for filters in route handlers in minimal applications. Filters are executed before the core route handler logic and can be used to inspect and modify handler parameters or intercept handler execution.<\/p>\n<p>Filters can be registered onto a handler using a variety of strategies. For example, you can register a filter using a <code>RouteHandlerFilterDelegate<\/code> and the <code>AddFilter<\/code> extension method as follows:<\/p>\n<pre><code class=\"language-csharp\">string HelloName(string name) =&gt; $\"Hello, {name}!\";\r\n\r\napp.MapGet(\"\/hello\/{name}\", HelloName)\r\n    .AddFilter(async (routeHandlerInvocationContext, next) =&gt;\r\n    {\r\n        var name = (string) routeHandlerInvocationContext.Parameters[0];\r\n        if (name == \"Bob\")\r\n        {\r\n            return Results.Problem(\"No Bob's allowed\");\r\n        }\r\n        return await next(routeHandlerInvocationContext);\r\n    });<\/code><\/pre>\n<p>Filters can also be registered via a filter factory strategy that gives access to a <code>RouteHandlerContext<\/code> which gives access to the <code>MethodInfo<\/code> associated with the handler and the metadata registered on the endpoint.<\/p>\n<pre><code class=\"language-csharp\">app.MapGet(\"\/hello\/{name}\", HelloName)\r\n    .AddFilter((routeHandlerContext, next) =&gt;\r\n    {\r\n        var parameters = routeHandlerContext.MethodInfo.GetParameters();\r\n        var hasCorrectSignature = parameters.Length == 1 && parameters[0].ParameterType == typeof(string);\r\n        return async (routeHandlerInvocationContext) =&gt;\r\n        {\r\n            if (hasCorrectSignature)\r\n            {\r\n                var name = (string) routeHandlerInvocationContext.Parameters[0];\r\n                if (name == \"Bob\")\r\n                {\r\n                    return Results.Problem(\"No Bob's allowed\");\r\n                }\r\n            }\r\n            return await next(routeHandlerInvocationContext);\r\n        };\r\n    });<\/code><\/pre>\n<p>Finally, filters can implement the <code>IRouteHandlerFilter<\/code> interface and be resolved from DI or passed as an instance.<\/p>\n<pre><code class=\"language-csharp\">app.MapGet(\"\/hello\/{name}\", HelloName)\r\n    .AddFilter&lt;MyFilter&gt;();<\/code><\/pre>\n<h2>Improved unit testability for minimal route handlers<\/h2>\n<p><code>IResult<\/code> implementation types are now publicly available in the namespace <code>Microsoft.AspNetCore.Http<\/code> with the suffix <code>HttpResult<\/code> (<code>OkObjectHttpResult<\/code>, <code>ProblemHttpResult<\/code>, etc.). With these types, you can now more easily unit test your minimal route handlers when using named methods instead of lambdas.<\/p>\n<pre><code class=\"language-csharp\">[Fact]\r\npublic async Task GetTodoReturnsTodoFromDatabase()\r\n{\r\n    var todo = new Todo { Id = 42, Name = \"Improve Results testability!\" };\r\n    var mockDb = new MockTodoDb(new[] { todo });\r\n\r\n    var result = (OkObjectHttpResult)await TodoEndpoints.GetTodo(mockDb, todo.Id);\r\n\r\n    \/\/Assert\r\n    Assert.Equal(200, result.StatusCode);\r\n\r\n    var foundTodo = Assert.IsAssignableFrom&lt;Models.Todo&gt;(result.Value);\r\n    Assert.Equal(id, foundTodo.Id);\r\n}\r\n\r\n[Fact]\r\npublic void CreateTodoWithValidationProblems()\r\n{\r\n    \/\/Arrange\r\n    var newTodo = default(Todo);\r\n    var mockDb = new MockTodoDb();\r\n\r\n    \/\/Act\r\n    var result = TodoEndpoints.CreateTodo(mockDb, newTodo);\r\n\r\n    \/\/Assert        \r\n    var problemResult = Assert.IsAssignableFrom&lt;ProblemHttpResult&gt;(result);\r\n    Assert.NotNull(problemResult.ProblemDetails);\r\n    Assert.Equal(400, problemResult.StatusCode);\r\n}<\/code><\/pre>\n<h2>Bind using <code>TryParse<\/code> in MVC and API Controllers<\/h2>\n<p>You can now bind controller action parameter values using a <code>TryParse<\/code> method that has one of the following signatures: <\/p>\n<pre><code class=\"language-csharp\">public static bool TryParse(string value, T out result);\r\npublic static bool TryParse(string value, IFormatProvider provider, T out result);<\/code><\/pre>\n<p>For example, the <code>Get<\/code> action in the following controller binds data from the query string using a <code>TryParse<\/code> method on the parameter type:<\/p>\n<pre><code class=\"language-csharp\">public class TryParseController : ControllerBase\r\n{\r\n    \/\/ GET \/tryparse?data=MyName\r\n    [HttpGet]\r\n    public ActionResult Get([FromQuery]CustomTryParseObject data) =&gt; Ok();\r\n\r\n    public class CustomTryParseObject\r\n    {\r\n        public string? Name { get; set; }\r\n\r\n        public static bool TryParse(string s, out CustomTryParseObject result)\r\n        {\r\n            if (s is null) \r\n            {\r\n                result = default;\r\n                return false;\r\n            }\r\n\r\n            result = new CustomTryParseObject { Name = s };\r\n            return true;\r\n        }\r\n    }\r\n}<\/code><\/pre>\n<h2>New <code>Results.Stream()<\/code> overloads<\/h2>\n<p>We introduced new <code>Results.Stream(...)<\/code> overloads to accommodate scenarios where you 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. The example below demonstrates using <code>Results.Stream()<\/code> when doing image manipulation with <a href=\"https:\/\/sixlabors.com\/products\/imagesharp\">ImageSharp<\/a>.<\/p>\n<pre><code class=\"language-csharp\">app.MapGet(\"\/process-image\", async (HttpContext http) =&gt;\r\n{\r\n    using var image = await Image.LoadAsync(\"puppy.jpeg\");\r\n    int width = image.Width \/ 2;\r\n    int height = image.Height \/ 2;\r\n    image.Mutate(x =&gt; x.Resize(width, height));\r\n    http.Response.Headers.CacheControl = $\"public,max-age={FromHours(24).TotalSeconds}\";\r\n    return Results.Stream(stream =&gt; image.SaveAsync(stream, PngFormat.Instance), \"image\/png\");\r\n});<\/code><\/pre>\n<h2>Improved HTTP\/2 performance when using many streams on a connection<\/h2>\n<p>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. A gRPC benchmark with 70 streams on a single connection (with TLS) showed a ~15% improvement in requests per second (RPS) with this change.<\/p>\n<h2>New <code>ServerReady<\/code> event for measuring startup time<\/h2>\n<p>If you use <code>EventSource<\/code> for metrics\/diagnostics and want to measure the startup time of your ASP.NET Core app, you can now use the new <code>ServerReady<\/code> event in the <code>Microsoft.AspNetCore.Hosting<\/code> source that represents the point where your server is up and running.<\/p>\n<h2>Developer exception page dark mode<\/h2>\n<p>The ASP.NET Core developer exception page now supports dark mode:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2022\/04\/dev-exception-page-dark.png\" alt=\"ASP.NET Core developer exception page dark mode\" \/><\/p>\n<p>Thank you <a href=\"https:\/\/github.com\/poke\">@poke<\/a> for contributing this improvement!<\/p>\n<h2>Give feedback<\/h2>\n<p>We hope you enjoy this preview release of ASP.NET Core in .NET 7. Let us know what you think about these new improvements by filing issues on <a href=\"https:\/\/github.com\/dotnet\/aspnetcore\/issues\/new\">GitHub<\/a>.<\/p>\n<p>Thanks for trying out ASP.NET Core!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>.NET 7 Preview 3 is here and adds some great new features to ASP.NET Core including support for route handler filters in minimal APIs, improved unit testability for minimal route handlers, performance enhancements, and more.<\/p>\n","protected":false},"author":417,"featured_media":39402,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[685,197,7509,7251],"tags":[7611,32,7205,433],"class_list":["post-39401","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dotnet","category-aspnet","category-aspnetcore","category-blazor","tag-dotnet-7","tag-asp-net-core","tag-blazor","tag-preview"],"acf":[],"blog_post_summary":"<p>.NET 7 Preview 3 is here and adds some great new features to ASP.NET Core including support for route handler filters in minimal APIs, improved unit testability for minimal route handlers, performance enhancements, and more.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/39401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/417"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=39401"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/39401\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/39402"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=39401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=39401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=39401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}