Here is some code:
App.razor
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><base href="/" /><link rel="stylesheet" href="@Assets["app.css"]" /><link rel="stylesheet" href="@Assets["Web.styles.css"]" /><link rel="stylesheet" href="mainStyles.css" /><ImportMap /><HeadOutlet /></head><body><Routes /><script src="_framework/blazor.web.js"></script><script> window.Foo_JS = async function () { var obj = {}; obj.message = "Hello from JS"; return await new Promise((resolve, reject) => { resolve(JSON.stringify(obj)); }); }</script></body></html>Main.razor
@using System.Net.Http@using System.Net.Http.Json@using Microsoft.AspNetCore.Components.Forms@using Microsoft.AspNetCore.Components.Routing@using Microsoft.AspNetCore.Components.Web@using static Microsoft.AspNetCore.Components.Web.RenderMode@using Microsoft.AspNetCore.Components.Web.Virtualization@using Microsoft.JSInterop@using Microsoft.JSInterop.Implementation@inject IJSRuntime JSR@rendermode InteractiveServer@page "/"@code { public string fromStandardMethod { get { string task = Task<string>.Run(() => { return StandardMethod().Result; }).Result; return task; } } public string fromJSRInvokeAsync { get { string task = Task<string>.Run(() => { return JSRInvokeAsync().Result; }).Result; return task; } } // Works fine public async Task<string> StandardMethod() { await Task.Delay(100); return "Standard method"; } // System.AggregateException: One or more errors occurred. (One or more errors // occurred. (One or more errors occurred. (A task was canceled.))) public async Task<string> JSRInvokeAsync() { await Task.Delay(100); return await JSR.InvokeAsync<string>("window.Foo_JS"); } protected override async Task OnAfterRenderAsync(bool firstRender) { await JSR.InvokeVoidAsync("alert", fromStandardMethod); await JSR.InvokeVoidAsync("alert", fromJSRInvokeAsync); }}I try to get result string from IJSRuntime.InvokeAsync() in "getter", but something went wrong. When i call code without IJSRuntime.InvokeAsync() it works fine. What i doing wrong and how it can be fixed in my code?
Error in console:
Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111] Unhandled exception in circuit 'TFxKutHcuL9njLexzxgn2hVjqwJMTGbg638hLfyACk8'. System.AggregateException: One or more errors occurred. (One or more errors occurred. (A task was canceled.)) ---> System.AggregateException: One or more errors occurred. (A task was canceled.) ---> System.Threading.Tasks.TaskCanceledException: A task was canceled. at System.Threading.Tasks.Task.GetExceptions(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification) at Web.WorkSpace.Pages.Main.<get_fromJSRInvokeAsync>b__4_0() in C:\Users\Shadow\Desktop\Web\Web\WorkSpace\Pages\Main.razor:line 38