Questions tagged [iasyncdisposable]
16 questions
189
votes
2 answers
What is the difference between using and await using? And how can I decide which one to use?
I've noticed that in some case, Visual Studio recommends to do this
await using var disposable = new Disposable();
// Do something
Instead of this
using var disposable = new Disposable();
// Do something
What is the difference between using and…

Justin Lessard
- 10,804
- 5
- 49
- 61
22
votes
4 answers
Proper way to deal with exceptions in DisposeAsync
During switching to the new .NET Core 3's IAsynsDisposable, I've stumbled upon the following problem.
The core of the problem: if DisposeAsync throws an exception, this exception hides any exceptions thrown inside await using-block.
class Program…

Vlad
- 35,022
- 6
- 77
- 199
12
votes
2 answers
What's the recommended way to deal with leaked IAsyncDisposable instances?
I've been familiarizing myself with some of the things (that are planned to be) added in C# 8 & .NET Core 3.0, and am unsure on the correct way to implement IAsyncDisposable (at time of writing, this link has literally no guidance).
In particular,…

Kevin Montrose
- 22,191
- 9
- 88
- 137
10
votes
2 answers
How do I get the "await using" syntax correct?
I have the following synchronous code, which works fine:
private void GenerateExportOutput()
{
using StreamWriter writer = new(Coordinator.OutputDirectory + @"\export.txt");
if (this.WikiPagesToExport.IsEmpty)
{
…

Bob Vesterman
- 1,127
- 1
- 11
- 31
6
votes
1 answer
How to safely dispose of IAsyncDisposable objects retrieved with await foreach?
Can I do this using using var:
await foreach (var response in _container.GetItemQueryStreamIterator(query))
{
using var safeResponse = response;
//use the safeResponse
}
or should I do this:
await foreach (var response in…

fred_
- 1,486
- 1
- 19
- 31
5
votes
3 answers
Implementing both IDisposable and IAsyncDisposable
Say I have a non-sealed class that does not deal with any unmanaged resources. I need to make a single async call during its disposing stage to do some clean up. There are no other managed resources to deal with.
From what I understand, in order to…

millie
- 2,642
- 10
- 39
- 58
3
votes
1 answer
IAsyncDisposable with Using statement in VB.NET
I have a class with a resource that ideally should be disposed of using an async method, and I'm trying to use a Using statement to do so.
Luckily, .NET added the IAsyncDisposable interface as well as a nice doc explaining how to Implement a…

Keith Stein
- 6,235
- 4
- 17
- 36
3
votes
2 answers
ConfigureAwait(false) and struct implementation of IAsyncDisposable
I have implemented IAsyncDisposable with an ActionOnAsyncDispose struct as shown below. My understanding is that the compiler will not box it when it is in an async using statement:
ActionOnDisposeAsync x = ...;
await using (x) {
…

sjb-sjb
- 1,112
- 6
- 14
3
votes
0 answers
How to deal with IAsyncDisposable and IDisposable interfaces when no synchronous dispose available?
I am writing a Connection like class around an interop object. In particular, the closing of my connection is asynchronous.
The object I am interfacing with has a very poor API, like:
public interface InteropObject
{
void StartLongTask();
…

fharreau
- 2,105
- 1
- 23
- 46
3
votes
1 answer
Correct disposal in IAsyncEnumerable methods?
The answer to this may be that it's not possible, but the question is: assume you have a C# method for consuming the lines in a TextReader that returns IAsyncEnumerable. How do you ensure that when DisposeAsync is called on the…

Julian Birch
- 2,605
- 1
- 21
- 36
1
vote
1 answer
How to dispose a service implementing IAsyncDisposable using Dependency Injection in .NET 7
I am having a service that uses the IAsyncDisposable interface, because it holds an object that has the interface itself.
public class AddressImporter : IAddressImporter
{
private readonly IConsumer _consumer; // implementing…

TeaJ
- 25
- 4
1
vote
1 answer
Inheriting from Stephen Cleary's Nito.Disposables NuGet package
I saw the implementation of Stephen Cleary's Disposables NuGet package and it seems like it's perfect in my case, even tho, I couldn't find examples on how to inherit from it.
My idea is to make UnsubscribeAsync().GetAwaiter().GetResult(); to await…

nop
- 4,711
- 6
- 32
- 93
0
votes
0 answers
MEF vs. IAsyncDisposable or iterate over all instantiated exports
I've shared MEF exports that implement IAsyncDisposable.
If an export in MEF implements IDisposable it will be disposed when the composition container (or maybe the catalog) is disposed.
IAsyncDisposable is not recognized from MEF. Is there any…

Sebastian Schumann
- 3,204
- 19
- 37
0
votes
0 answers
How do IDisposable and IAsyncDisposable both work together?
The class below is not sealed, which means it is considered inheritable. I took IDisposable/IAsyncDisposable implementation from here and I'm trying to understand why the .Dispose calls are duplicated in both Dispose and DisposeAsync. Will someone…

nop
- 4,711
- 6
- 32
- 93
0
votes
1 answer
Should I call IAsyncDisposable.DisposeAsync() in Program.cs using JS module?
I need to configure localization in Blazor WASM, .NET 6
According to the manual, I need to use JavaScript in the Program.cs file. Microsoft recommends connecting JavaScript code through JavaScript isolation in JavaScript modules. However, the code…

Евгений Ляшенко
- 177
- 1
- 1
- 8