From 571d47d1427ce26b51633ad095c5cf3233a29673 Mon Sep 17 00:00:00 2001 From: Tanay Parikh Date: Mon, 21 Mar 2022 11:34:30 -0700 Subject: [PATCH] Forward Port Update MAUI Exception Handling (#40765) (cherry picked from commit 7eb15474ba995903ee9c8be0eb2b7a75b8813719) --- src/Components/README.md | 1 + src/Components/WebView/WebView/src/IpcSender.cs | 6 ++++++ .../WebView/WebView/src/Services/WebViewRenderer.cs | 4 +--- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Components/README.md b/src/Components/README.md index 3f8687dee6f5..c787c4c90b27 100644 --- a/src/Components/README.md +++ b/src/Components/README.md @@ -25,6 +25,7 @@ The following contains a description of each sub-directory in the `Components` d - `Server`: Contains the implementation for WASM-specific extension methods and the launch logic for the debugging proxy - `WebAssembly`: Contains WebAssembly-specific implementations of the renderer, HostBuilder, etc. - `WebAssembly.Authentication`: Contains the WASM-specific implementations +- `WebView`: Contains the source files to support [Blazor Hybrid](https://github.com/dotnet/maui/tree/main/src/BlazorWebView) within [`dotnet/maui`](https://github.com/dotnet/maui). Changes in this project can be tested with `dotnet/maui` following [this guide](https://github.com/dotnet/maui/wiki/Blazor-Desktop#aspnet-core). ## Development Setup diff --git a/src/Components/WebView/WebView/src/IpcSender.cs b/src/Components/WebView/WebView/src/IpcSender.cs index 697702dfadec..bba6725f2df3 100644 --- a/src/Components/WebView/WebView/src/IpcSender.cs +++ b/src/Components/WebView/WebView/src/IpcSender.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics.CodeAnalysis; +using System.Runtime.ExceptionServices; +using System.Threading.Tasks; using Microsoft.AspNetCore.Components.RenderTree; using Microsoft.JSInterop; @@ -60,8 +62,12 @@ public void SendByteArray(int id, byte[] data) public void NotifyUnhandledException(Exception exception) { + // Send the serialized exception to the WebView for display var message = IpcCommon.Serialize(IpcCommon.OutgoingMessageType.NotifyUnhandledException, exception.Message, exception.StackTrace); _dispatcher.InvokeAsync(() => _messageDispatcher(message)); + + // Also rethrow so the AppDomain's UnhandledException handler gets notified + _dispatcher.InvokeAsync(() => ExceptionDispatchInfo.Capture(exception).Throw()); } private void DispatchMessageWithErrorHandling(string message) diff --git a/src/Components/WebView/WebView/src/Services/WebViewRenderer.cs b/src/Components/WebView/WebView/src/Services/WebViewRenderer.cs index 7204b55eaec7..e5dfcbb59e9e 100644 --- a/src/Components/WebView/WebView/src/Services/WebViewRenderer.cs +++ b/src/Components/WebView/WebView/src/Services/WebViewRenderer.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Runtime.ExceptionServices; +using System.Text.Json; using Microsoft.AspNetCore.Components.RenderTree; using Microsoft.AspNetCore.Components.Web.Infrastructure; using Microsoft.Extensions.Logging; @@ -36,9 +37,6 @@ protected override void HandleException(Exception exception) { // Notify the JS code so it can show the in-app UI _ipcSender.NotifyUnhandledException(exception); - - // Also rethrow so the AppDomain's UnhandledException handler gets notified - ExceptionDispatchInfo.Capture(exception).Throw(); } protected override Task UpdateDisplayAsync(in RenderBatch renderBatch)