Skip to content

Improve Components error handling #7165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Blazor.Rendering;
using Microsoft.AspNetCore.Components.Builder;

Expand Down Expand Up @@ -35,13 +36,13 @@ public void AddComponent(Type componentType, string domElementSelector)
Entries.Add((componentType, domElementSelector));
}

public WebAssemblyRenderer CreateRenderer()
public async Task<WebAssemblyRenderer> CreateRendererAsync()
{
var renderer = new WebAssemblyRenderer(Services);
for (var i = 0; i < Entries.Count; i++)
{
var entry = Entries[i];
renderer.AddComponent(entry.componentType, entry.domElementSelector);
var (componentType, domElementSelector) = Entries[i];
await renderer.AddComponentAsync(componentType, domElementSelector);
}

return renderer;
Expand Down
10 changes: 6 additions & 4 deletions src/Components/Blazor/Blazor/src/Hosting/WebAssemblyHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public Task StartAsync(CancellationToken cancellationToken = default)
JSRuntime.SetCurrentJSRuntime(_runtime);
SetBrowserHttpMessageHandlerAsDefault();

return StartAsyncAwaited();
}

private async Task StartAsyncAwaited()
{
var scopeFactory = Services.GetRequiredService<IServiceScopeFactory>();
_scope = scopeFactory.CreateScope();

Expand All @@ -61,7 +66,7 @@ public Task StartAsync(CancellationToken cancellationToken = default)
var builder = new WebAssemblyBlazorApplicationBuilder(_scope.ServiceProvider);
startup.Configure(builder, _scope.ServiceProvider);

_renderer = builder.CreateRenderer();
_renderer = await builder.CreateRendererAsync();
}
catch
{
Expand All @@ -76,9 +81,6 @@ public Task StartAsync(CancellationToken cancellationToken = default)

throw;
}


return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken = default)
Expand Down
45 changes: 34 additions & 11 deletions src/Components/Blazor/Blazor/src/Rendering/WebAssemblyRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Browser;
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.JSInterop;
using Mono.WebAssembly.Interop;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.AspNetCore.Blazor.Rendering
{
Expand All @@ -24,7 +23,7 @@ public class WebAssemblyRenderer : Renderer
/// Constructs an instance of <see cref="WebAssemblyRenderer"/>.
/// </summary>
/// <param name="serviceProvider">The <see cref="IServiceProvider"/> to use when initializing components.</param>
public WebAssemblyRenderer(IServiceProvider serviceProvider): base(serviceProvider)
public WebAssemblyRenderer(IServiceProvider serviceProvider) : base(serviceProvider)
{
// The browser renderer registers and unregisters itself with the static
// registry. This works well with the WebAssembly runtime, and is simple for the
Expand All @@ -38,19 +37,26 @@ public WebAssemblyRenderer(IServiceProvider serviceProvider): base(serviceProvid
/// </summary>
/// <typeparam name="TComponent">The type of the component.</typeparam>
/// <param name="domElementSelector">A CSS selector that uniquely identifies a DOM element.</param>
public void AddComponent<TComponent>(string domElementSelector)
where TComponent: IComponent
{
AddComponent(typeof(TComponent), domElementSelector);
}
/// <returns>A <see cref="Task"/> that represents the asynchronous rendering of the added component.</returns>
/// <remarks>
/// Callers of this method may choose to ignore the returned <see cref="Task"/> if they do not
/// want to await the rendering of the added component.
/// </remarks>
public Task AddComponentAsync<TComponent>(string domElementSelector) where TComponent : IComponent
=> AddComponentAsync(typeof(TComponent), domElementSelector);

/// <summary>
/// Associates the <see cref="IComponent"/> with the <see cref="WebAssemblyRenderer"/>,
/// causing it to be displayed in the specified DOM element.
/// </summary>
/// <param name="componentType">The type of the component.</param>
/// <param name="domElementSelector">A CSS selector that uniquely identifies a DOM element.</param>
public void AddComponent(Type componentType, string domElementSelector)
/// <returns>A <see cref="Task"/> that represents the asynchronous rendering of the added component.</returns>
/// <remarks>
/// Callers of this method may choose to ignore the returned <see cref="Task"/> if they do not
/// want to await the rendering of the added component.
/// </remarks>
public Task AddComponentAsync(Type componentType, string domElementSelector)
{
var component = InstantiateComponent(componentType);
var componentId = AssignRootComponentId(component);
Expand All @@ -66,7 +72,7 @@ public void AddComponent(Type componentType, string domElementSelector)
domElementSelector,
componentId);

RenderRootComponent(componentId);
return RenderRootComponentAsync(componentId);
}

/// <inheritdoc />
Expand All @@ -93,5 +99,22 @@ protected override Task UpdateDisplayAsync(in RenderBatch batch)
throw new NotImplementedException($"{nameof(WebAssemblyRenderer)} is supported only with in-process JS runtimes.");
}
}

/// <inheritdoc />
protected override void HandleException(Exception exception)
{
Console.Error.WriteLine($"Unhandled exception rendering component:");
if (exception is AggregateException aggregateException)
{
foreach (var innerException in aggregateException.Flatten().InnerExceptions)
{
Console.Error.WriteLine(innerException);
}
}
else
{
Console.Error.WriteLine(exception);
}
}
}
}
5 changes: 5 additions & 0 deletions src/Components/Blazor/Build/test/RazorIntegrationTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,11 @@ public TestRenderer() : base(new TestServiceProvider(), CreateDefaultDispatcher(
public void AttachComponent(IComponent component)
=> AssignRootComponentId(component);

protected override void HandleException(Exception exception)
{
throw new NotImplementedException();
}

protected override Task UpdateDisplayAsync(in RenderBatch renderBatch)
{
LatestBatchReferenceFrames = renderBatch.ReferenceFrames.ToArray();
Expand Down
19 changes: 5 additions & 14 deletions src/Components/Browser.JS/src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions src/Components/Browser/src/RendererRegistryEventDispatcher.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.JSInterop;
using System;

namespace Microsoft.AspNetCore.Components.Browser
{
Expand All @@ -16,12 +17,13 @@ public static class RendererRegistryEventDispatcher
/// For framework use only.
/// </summary>
[JSInvokable(nameof(DispatchEvent))]
public static void DispatchEvent(
public static Task DispatchEvent(
BrowserEventDescriptor eventDescriptor, string eventArgsJson)
{
var eventArgs = ParseEventArgsJson(eventDescriptor.EventArgsType, eventArgsJson);
var renderer = RendererRegistry.Current.Find(eventDescriptor.BrowserRendererId);
renderer.DispatchEvent(

return renderer.DispatchEventAsync(
eventDescriptor.ComponentId,
eventDescriptor.EventHandlerId,
eventArgs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public FakeRenderer()
{
}

protected override void HandleException(Exception exception)
{
throw new NotImplementedException();
}

protected override Task UpdateDisplayAsync(in RenderBatch renderBatch)
=> Task.CompletedTask;
}
Expand Down
Loading