@@ -76,6 +76,8 @@ public Task DispatchEventAsync(
7676
7777 var result = Dispatcher . InvokeAsync ( ( ) =>
7878 {
79+ ResetUnhandledException ( ) ;
80+
7981 try
8082 {
8183 return base . DispatchEventAsync ( eventHandlerId , fieldInfo , eventArgs ) ;
@@ -127,6 +129,8 @@ public void DisposeComponents()
127129 // will only work on IDisposable
128130 var disposeTask = Dispatcher . InvokeAsync ( ( ) =>
129131 {
132+ ResetUnhandledException ( ) ;
133+
130134 foreach ( var root in rootComponents )
131135 {
132136 root . Detach ( ) ;
@@ -142,20 +146,6 @@ public void DisposeComponents()
142146 AssertNoUnhandledExceptions ( ) ;
143147 }
144148
145- /// <inheritdoc/>
146- protected override void HandleException ( Exception exception )
147- {
148- capturedUnhandledException = exception ;
149-
150- logger . LogUnhandledException ( capturedUnhandledException ) ;
151-
152- if ( ! unhandledExceptionTsc . TrySetResult ( capturedUnhandledException ) )
153- {
154- unhandledExceptionTsc = new TaskCompletionSource < Exception > ( ) ;
155- unhandledExceptionTsc . SetResult ( capturedUnhandledException ) ;
156- }
157- }
158-
159149 /// <inheritdoc/>
160150 protected override Task UpdateDisplayAsync ( in RenderBatch renderBatch )
161151 {
@@ -219,10 +209,10 @@ protected override void Dispose(bool disposing)
219209 private TResult Render < TResult > ( RenderFragment renderFragment , Func < int , TResult > activator )
220210 where TResult : IRenderedFragmentBase
221211 {
222- ResetUnhandledException ( ) ;
223-
224212 var renderTask = Dispatcher . InvokeAsync ( ( ) =>
225213 {
214+ ResetUnhandledException ( ) ;
215+
226216 var root = new RootComponent ( renderFragment ) ;
227217 var rootComponentId = AssignRootComponentId ( root ) ;
228218 var result = activator ( rootComponentId ) ;
@@ -350,7 +340,30 @@ private ArrayRange<RenderTreeFrame> GetOrLoadRenderTreeFrame(RenderTreeFrameDict
350340 return framesCollection [ componentId ] ;
351341 }
352342
353- private void ResetUnhandledException ( ) => capturedUnhandledException = null ;
343+ /// <inheritdoc/>
344+ protected override void HandleException ( Exception exception )
345+ {
346+ if ( exception is null )
347+ return ;
348+
349+ logger . LogUnhandledException ( exception ) ;
350+
351+ capturedUnhandledException = exception ;
352+
353+ if ( ! unhandledExceptionTsc . TrySetResult ( capturedUnhandledException ) )
354+ {
355+ unhandledExceptionTsc = new TaskCompletionSource < Exception > ( ) ;
356+ unhandledExceptionTsc . SetResult ( capturedUnhandledException ) ;
357+ }
358+ }
359+
360+ private void ResetUnhandledException ( )
361+ {
362+ capturedUnhandledException = null ;
363+
364+ if ( unhandledExceptionTsc . Task . IsCompleted )
365+ unhandledExceptionTsc = new TaskCompletionSource < Exception > ( ) ;
366+ }
354367
355368 private void AssertNoUnhandledExceptions ( )
356369 {
0 commit comments