ASP.Net Core Razor components: await vs .Result

Consider the following two lines in View file for ASP.Net Core:

    @Component.InvokeAsync("Greeting").Result
    @await Component.InvokeAsync("Greeting")

What is the difference? Which is preferable and in which situation? In my code they both give the same visual result; are there cases where results could be different?

Both calls most likely takes the same time to return, the difference is that the first call will block the thread but the second call will return the thread to the pool and allow it to be used by another request. Essentially meaning that your Service can handle more requests per second when using the await keyword.

Use await where possible to prevent the thread from blocking. Do some reading on async and await - https://docs.microsoft.com/en-us/dotnet/csharp/async

From MSDN - The await keyword is where the magic happens. It yields control to the caller of the method that performed await, and it ultimately allows a UI to be responsive or a service to be elastic.

Essentially the await keyword yields control back to the caller and releases the thread back into the threadpool.

Create and use ASP.NET Core Razor components, Both implicit and explicit Razor expressions result in the following error if It isn't mentioned in the component docs or the general Razor syntax doc how to do asynchronous work in Blazor as part of : dotnet/AspNetCore. If you're not using await, then calling Result/Wait will lock the thread until complete, so unless something else in your application is creating other threads, it may only take one usage of Result or Wait to lock the application. One of many reasons why standardizing on using await is the right answer. – Aaron M. Eshbach Sep 27 '19 at 16:09

NB: Razor Components are something different - this is server-side hosted Blazor, part of .NET Core 3.0. What you mean, however, is ASP.NET Core x.x View Components.

View components in ASP.NET Core, View components are recommended instead of partial pages or tag helpers whenever any form of logic is required to obtain data for inclusion in the resulting HTML snippet, specifically calls to an external var endPoint = "https://​jsonplaceholder.typicode.com/users";; var json = await client. AspNetCore. Invoking a view component as a Tag Helper. For ASP.NET Core 1.1 and higher, you can invoke a view component as a Tag Helper: Pascal-cased class and method parameters for Tag Helpers are translated into their kebab case. The Tag Helper to invoke a view component uses the <vc></vc> element.

Calling .Result will result in a blocking call which could cause issues with your UI. If you're calling an asynchronous function, you should await it to allow the rest of your application to continue without being blocked.

Razor components: error using await in Razor expressions (Preview , In this post I take a brief look at Razor Components, and whether you should consider which have the pretty much direct equivalent of Html Helpers from "​classic" ASP. NET Core app, where you want to render something different I won't get into Blazor Server vs Blazor WebAssembly here, as it's not  To prepare a Razor Pages or MVC app to host Razor components, follow the guidance in the Integrate Razor components into Razor Pages and MVC apps section of the ASP.NET Core Blazor hosting models article. When using a custom folder to hold the app's components, add the namespace representing the folder to either the page/view or to the

Calling .Result on the component makes you lose the essence of the await which is suppose to prevent blocking your UI as the code does not need to wait for the complete execution of the Razor component.

Hence it is advisable not to call the .Result rather you should await the result to allow the complete asynchronous execution flow of your code.

View Components in Razor Pages, The first issue is at a pretty high level: You can no longer pass parameters from the View or Razor Page hosting your component to the  Introducing view components ¶ New to ASP.NET Core MVC, view components are similar to partial views, but they are much more powerful. View components don’t use model binding, and only depend on the data you provide when calling into it. A view component: Renders a chunk rather than a whole response

Don't replace your View Components with Razor Components, Note: Razor Components is now called Blazor Server. They're new, a bit weird when you first see them and they may or may not change how we're all building ASP. the corner) I figured I'd take them for a spin and document the results here​. NET Core 3 Preview 3 (or higher), you'll get a new “Razor  Razor syntax reference for ASP.NET Core. 02/12/2020; 18 minutes to read +12; In this article. By Rick Anderson, Taylor Mullen, and Dan Vicarel. Razor is a markup syntax for embedding server-based code into webpages. The Razor syntax consists of Razor markup, C#, and HTML. Files containing Razor generally have a .cshtml file extension.

for Blazor in .NET Core 3.0, In .NET or .NET Core world, applications can work asynchronously using async await keyword. In particular, it's usually a bad idea to block on async code by calling Task.Wait or Task.Result. NET Core Razor Components. ASP.NET Core Blazor event handling. 03/16/2020; 5 minutes to read; In this article. By Luke Latham and Daniel Roth. Razor components provide event handling features. For an HTML element attribute named @on{EVENT} (for example, @onclick) with a delegate-typed value, a Razor component treats the attribute's value as an event handler.

A gentle introduction to Blazor Server - jonhilton.net, NET Core MVC, view components are similar to partial views, but they are much more when creating the view component result or when calling the View method. public async Task<IViewComponentResult> InvokeAsync( int maxPriority, bool Create a Views/Shared/Components/PriorityList/Default.​cshtml Razor view. Elements of a view component. View components consist of a class file and a .cshtml view file. The class file contains the logic for generating the model. It can be though of as a mini-controller, just as the Razor PageModel file is considered to be a controller.

Comments
  • Possible duplicate of Await on a completed task same as task.Result?