Blazor

Why do tasks returned from JSRuntime.InvokeAsync during OnInitAsync never finish in Blazor?

Why do tasks returned from JSRuntime.InvokeAsync during OnInitAsync never finish in Blazor?

This is a known bug and is resolved in the upcoming release of Blazor Preview 3. [Issue Resolved]

[wwwroot/index.html]

<script>
        window.interopDuringOnInit = function () {
            return "Hello World";
        }
</script>

[Razor]

@code {
    string Greeting;

    protected override async Task OnInitializedAsync()
    {
        try
        {            
            Greeting = await JSRuntime.InvokeAsync<string>("interopDuringOnInit");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Could not invoke interop during OnInit, " + ex.ToString());
        }
    }
}

I would suggest you check the below link for better understanding:
https://github.com/aspnet/AspNetCore/issues/8274

Sumitra Kumari Shaw

An Engineering graduate who Enjoys building things that live on the internet.