public static WebAssemblyHostBuilder CreateDefault(string[]?args = default) { // We don't use the args for anything right now, but we want to accept them // here so that it shows up this way in the project templates. args ??= Array.Empty <string>(); var builder = new WebAssemblyHostBuilder(DefaultWebAssemblyJSRuntime.Instance); WebAssemblyCultureProvider.Initialize(); // Right now we don't have conventions or behaviors that are specific to this method // however, making this the default for the template allows us to add things like that // in the future, while giving `new WebAssemblyHostBuilder` as an opt-out of opinionated // settings. return(builder); }
// This method returns void because currently the JS side is not listening to any result, // nor will it handle any exceptions. We handle all exceptions internally to this method. // In the future we may want Blazor.start to return something that exposes the possibly-async // entrypoint result to the JS caller. There's no requirement to do that today, and if we // do change this it will be non-breaking. public static async void InvokeEntrypoint(string assemblyName, string[] args) { WebAssemblyCultureProvider.Initialize(); try { var assembly = Assembly.Load(assemblyName); var entrypoint = FindUnderlyingEntrypoint(assembly); var @params = entrypoint.GetParameters().Length == 1 ? new object[] { args ?? Array.Empty <string>() } : new object[] { }; var result = entrypoint.Invoke(null, @params); if (result is Task resultTask) { // In the default case, this Task is backed by the WebAssemblyHost.RunAsync that never completes. // Awaiting it is allows catching any exception thrown by user code in MainAsync. await resultTask; } } catch (Exception syncException) { HandleStartupException(syncException); return; } }