question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support IAsyncDisposable

See original GitHub issue

Is your feature request related to a problem? Please describe.

I implement IDisposable in some components to interop with JS, to clean up resources on the JS end. Blazor calls Dispose() on my component as appropriate.

However, this requires using IJSInProcessRuntime to get synchronous JS invocations.

Describe the solution you’d like

If the component implements IAsyncDisposable, Blazor should call (await) DisposeAsync.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:22
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

0reactions
javiercncommented, Jul 9, 2020

Async disposable support for Blazor

Currently we only support disposing components synchronously. For disposable components there are several design decissions that we’ve made along the way:

  • Within a render batch we process the disposed child components for a given component as a bundle, and produce a list of errors each component we process.
  • We don’t however, stop the render batch from running. Instead we simply notify the error and let the renderer deal with it within its handle error implementation.
    • RemoteRenderer will terminate the circuit.
    • WebAssemblyRenderer will log the error to the console.
  • A component being disposed can trigger a render somewhere else due to some notification during disposal, for example.

All these behaviors happen synchronously, so that means that a render batch only finishes rendering after all the components have been disposed and there are no more renders to process.

Supporting async disposal brings in several questions:

  • Do we wait for disposed child components within a given component render to finish disposing before continuing the work?
  • Do we wait to send UI updates until all components have successfully been disposed asynchronously?
  • Do we trigger new render batches after each component has completed disposing or do we wait until all of the components disposed in a previous batch have finished disposing?

Overview of the design

  • We want disposal to behave in a similar way as event handlers and lifecycle methods.
  • If as part of disposing an async disposable component a render is triggered within the syncrhonous piece, that render belongs in the same render batch being currently processed.
  • Otherwise, it belongs in a separate render batch.
  • Synchronous exceptions within async disposables are reported right away.
  • Asynchronous exceptions are reported once the task completes.
  • We report all the asynchronous disposal errors for a single render batch at the same time.
    • This simplifies processing, but we can also group them by component instead.
    • That said, synchronous errors will be reported before asynchronous errors are so as to not wait for them to complete.
Read more comments on GitHub >

github_iconTop Results From Across the Web

IAsyncDisposable Interface (System)
The IAsyncDisposable.DisposeAsync method of this interface returns a ValueTask that represents the asynchronous dispose operation.
Read more >
Implementing both IDisposable and IAsyncDisposable
Proxy might not support IAsyncDisposable . Failing to cast object to IDisposable will present hard to catch bugs in your app.
Read more >
How to work with IAsyncDisposable in .NET 6
Take advantage of the IAsyncDisposable interface to dispose of objects in a non-blocking way and make your .NET applications more efficient.
Read more >
How to properly use IAsyncDisposable in C#8
How to implement IAsyncDisposable. Basically, the simplest way to implement IAsyncDisposable is the following: public class Example : ...
Read more >
ASP.​NET Core in .NET 6 - Support for IAsyncDisposable ...
In this post, I want to have a look at the Support for IAsyncDisposable in MVC. The IAsyncDisposable is a thing since .NET...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found