Beispiel #1
0
        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);
        }
Beispiel #2
0
        public void ConventionBasedStartup_Configure()
        {
            // Arrange
            var instance = new MyStartup8();
            var startup  = new ConventionBasedStartup(instance);

            var services = new ServiceCollection().AddSingleton("foo").BuildServiceProvider();
            var builder  = new WebAssemblyBlazorApplicationBuilder(services);

            // Act
            startup.Configure(builder, services);

            // Assert
            Assert.Collection(
                instance.Arguments,
                a => Assert.Same(builder, a),
                a => Assert.Equal("foo", a));
        }