/// <summary> /// Disposes the host asynchronously. /// </summary> /// <returns>A <see cref="ValueTask"/> which respresents the completion of disposal.</returns> public async ValueTask DisposeAsync() { if (_disposed) { return; } _disposed = true; _renderer?.Dispose(); if (_scope is IAsyncDisposable asyncDisposableScope) { await asyncDisposableScope.DisposeAsync(); } else { _scope?.Dispose(); } if (_services is IAsyncDisposable asyncDisposableServices) { await asyncDisposableServices.DisposeAsync(); } else if (_services is IDisposable disposableServices) { disposableServices.Dispose(); } }
public Task StartAsync(CancellationToken cancellationToken = default) { // We need to do this as early as possible, it eliminates a bunch of problems. Note that what we do // is a bit fragile. If you see things breaking because JSRuntime.Current isn't set, then it's likely // that something on the startup path went wrong. // // We want to the JSRuntime created here to be the 'ambient' runtime when JS calls back into .NET. When // this happens in the browser it will be a direct call from Mono. We effectively needs to set the // JSRuntime in the 'root' execution context which implies that we want to do as part of a direct // call from Program.Main, and before any 'awaits'. JSRuntime.SetCurrentJSRuntime(_runtime); SetBrowserHttpMessageHandlerAsDefault(); var scopeFactory = Services.GetRequiredService <IServiceScopeFactory>(); _scope = scopeFactory.CreateScope(); try { var startup = _scope.ServiceProvider.GetService <IBlazorStartup>(); if (startup == null) { var message = $"Could not find a registered Blazor Startup class. " + $"Using {nameof(IWebAssemblyHost)} requires a call to {nameof(IWebAssemblyHostBuilder)}.UseBlazorStartup."; throw new InvalidOperationException(message); } // Note that we differ from the WebHost startup path here by using a 'scope' for the app builder // as well as the Configure method. var builder = new WebAssemblyBlazorApplicationBuilder(_scope.ServiceProvider); startup.Configure(builder, _scope.ServiceProvider); _renderer = builder.CreateRenderer(); } catch { _scope.Dispose(); _scope = null; if (_renderer != null) { _renderer.Dispose(); _renderer = null; } throw; } return(Task.CompletedTask); }
public Task StopAsync(CancellationToken cancellationToken = default) { if (_scope != null) { _scope.Dispose(); _scope = null; } if (_renderer != null) { _renderer.Dispose(); _renderer = null; } return(Task.CompletedTask); }
private async Task StartAsyncAwaited() { var scopeFactory = Services.GetRequiredService <IServiceScopeFactory>(); _scope = scopeFactory.CreateScope(); try { var startup = _scope.ServiceProvider.GetService <IBlazorStartup>(); if (startup == null) { var message = $"Could not find a registered Blazor Startup class. " + $"Using {nameof(IWebAssemblyHost)} requires a call to {nameof(IWebAssemblyHostBuilder)}.UseBlazorStartup."; throw new InvalidOperationException(message); } // Note that we differ from the WebHost startup path here by using a 'scope' for the app builder // as well as the Configure method. var builder = new WebAssemblyBlazorApplicationBuilder(_scope.ServiceProvider); startup.Configure(builder, _scope.ServiceProvider); _renderer = await builder.CreateRendererAsync(); } catch { _scope.Dispose(); _scope = null; if (_renderer != null) { _renderer.Dispose(); _renderer = null; } throw; } }