Skip to content

Blazor Async Main and Top-Level Statements Seems to Have Issues #47404

@JasonBock

Description

@JasonBock

Describe the bug

If I use top-level statements for a Blazor WebAssembly application, the application seems to work, but I get an unhandled exception warning.

To Reproduce

I was getting this issue when I was updating code I use for demos to .NET 5.0, which is here. For a simple reproduction, do this:

  • Create a new Blazor WebAssembly application targeting .NET 5.0 with no authorization, no HTTPS configuration, etc.
  • Don't change anything in the code. Just run the app, and the standard Blazor app template should show up in the browser.
  • Now, change the code in Program.cs to this:
using BlazorApp1;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Net.Http;

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

await builder.Build().RunAsync();

Note that this will force you to update the first line in App.razor to reference another type in the project, something like this:

<Router AppAssembly="@typeof(App).Assembly" PreferExactMatches="@true">

Exceptions (if any)

Now, when you run the app, it should still work, but you should see the yellow bar at the bottom saying "An unhandled error has occurred. Reload".

The console contains this exception information:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
d.printErr @ blazor.webassembly.js:1
blazor.webassembly.js:1  ---> System.PlatformNotSupportedException: Cannot wait on monitors on this runtime.
d.printErr @ blazor.webassembly.js:1
blazor.webassembly.js:1    at System.Threading.Monitor.ObjWait(Boolean exitContext, Int32 millisecondsTimeout, Object obj)
d.printErr @ blazor.webassembly.js:1
blazor.webassembly.js:1    at System.Threading.Monitor.Wait(Object obj, Int32 millisecondsTimeout, Boolean exitContext)
d.printErr @ blazor.webassembly.js:1
blazor.webassembly.js:1    at System.Threading.Monitor.Wait(Object obj, Int32 millisecondsTimeout)
d.printErr @ blazor.webassembly.js:1
blazor.webassembly.js:1    at System.Threading.ManualResetEventSlim.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
d.printErr @ blazor.webassembly.js:1
blazor.webassembly.js:1    at System.Threading.Tasks.Task.SpinThenBlockingWait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
d.printErr @ blazor.webassembly.js:1
blazor.webassembly.js:1    at System.Threading.Tasks.Task.InternalWaitCore(Int32 millisecondsTimeout, CancellationToken cancellationToken)
d.printErr @ blazor.webassembly.js:1
blazor.webassembly.js:1    at System.Threading.Tasks.Task.InternalWait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
d.printErr @ blazor.webassembly.js:1
blazor.webassembly.js:1    at <Program>$.<Main>(String[] args)
d.printErr @ blazor.webassembly.js:1
blazor.webassembly.js:1    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
d.printErr @ blazor.webassembly.js:1
blazor.webassembly.js:1    --- End of inner exception stack trace ---
d.printErr @ blazor.webassembly.js:1
blazor.webassembly.js:1    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
d.printErr @ blazor.webassembly.js:1
blazor.webassembly.js:1    at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
d.printErr @ blazor.webassembly.js:1
blazor.webassembly.js:1    at Microsoft.AspNetCore.Components.WebAssembly.Hosting.EntrypointInvoker.InvokeEntrypoint(String assemblyName, String[] args)
d.printErr @ blazor.webassembly.js:1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions