Skip to content

Commit be1d1e3

Browse files
authored
Merge pull request #1684 from danielmarbach/disposable
AsyncDisposable
2 parents 13fa6b2 + 68cc34e commit be1d1e3

40 files changed

+1383
-1513
lines changed

projects/RabbitMQ.Client.OAuth2/RabbitMQ.Client.OAuth2.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
https://learn.microsoft.com/en-us/answers/questions/1371494/for-net-standard-2-0-library-why-add-net-core-3-1
3232
https://devblogs.microsoft.com/dotnet/embracing-nullable-reference-types/#what-should-library-authors-do
3333
-->
34-
<LangVersion>8.0</LangVersion>
34+
<LangVersion>9.0</LangVersion>
3535
<Nullable>enable</Nullable>
3636
</PropertyGroup>
3737

projects/RabbitMQ.Client.OpenTelemetry/RabbitMQ.Client.OpenTelemetry.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
2828
<PackageOutputPath>../../packages</PackageOutputPath>
2929
<PackageReadmeFile>README.md</PackageReadmeFile>
30-
<LangVersion>7.3</LangVersion>
30+
<LangVersion>9.0</LangVersion>
3131
</PropertyGroup>
3232

3333
<PropertyGroup Condition="'$(Configuration)' == 'Release' And '$(CI)' == 'true'">

projects/RabbitMQ.Client/IChannel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ namespace RabbitMQ.Client
4242
/// functionality offered by versions 0-8, 0-8qpid, 0-9 and 0-9-1 of AMQP.
4343
/// </summary>
4444
/// <remarks>
45-
/// Extends the <see cref="IDisposable"/> interface, so that the "using"
45+
/// Extends the <see cref="IDisposable"/> interface and the <see cref="IAsyncDisposable"/> interface, so that the "using"
4646
/// statement can be used to scope the lifetime of a channel when appropriate.
4747
/// </remarks>
48-
public interface IChannel : IDisposable
48+
public interface IChannel : IAsyncDisposable, IDisposable
4949
{
5050
/// <summary>
5151
/// Channel number, unique per connections.

projects/RabbitMQ.Client/IConnection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ namespace RabbitMQ.Client
5050
/// Alternatively, an API tutorial can be found in the User Guide.
5151
/// </para>
5252
/// <para>
53-
/// Extends the <see cref="IDisposable"/> interface, so that the "using"
53+
/// Extends the <see cref="IDisposable"/> and the <see cref="IAsyncDisposable"/> interface, so that the "using"
5454
/// statement can be used to scope the lifetime of a connection when
5555
/// appropriate.
5656
/// </para>
5757
/// </remarks>
58-
public interface IConnection : INetworkConnection, IDisposable
58+
public interface IConnection : INetworkConnection, IAsyncDisposable, IDisposable
5959
{
6060
/// <summary>
6161
/// The maximum channel number this connection supports (0 if unlimited).

projects/RabbitMQ.Client/Impl/AutorecoveringChannel.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,9 @@ await _connection.DeleteRecordedChannelAsync(this,
257257
public override string ToString()
258258
=> InnerChannel.ToString();
259259

260-
public void Dispose()
260+
public void Dispose() => DisposeAsync().AsTask().GetAwaiter().GetResult();
261+
262+
public async ValueTask DisposeAsync()
261263
{
262264
if (_disposed)
263265
{
@@ -266,7 +268,8 @@ public void Dispose()
266268

267269
if (IsOpen)
268270
{
269-
this.AbortAsync().GetAwaiter().GetResult();
271+
await this.AbortAsync()
272+
.ConfigureAwait(false);
270273
}
271274

272275
_recordedConsumerTags.Clear();

projects/RabbitMQ.Client/Impl/AutorecoveringConnection.Recovery.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,12 @@ private async ValueTask RecoverExchangesAsync(IConnection connection,
299299
{
300300
try
301301
{
302-
using (IChannel ch = await connection.CreateChannelAsync(cancellationToken: cancellationToken).ConfigureAwait(false))
302+
IChannel channel = await connection.CreateChannelAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
303+
await using (channel.ConfigureAwait(false))
303304
{
304-
await recordedExchange.RecoverAsync(ch, cancellationToken)
305+
await recordedExchange.RecoverAsync(channel, cancellationToken)
305306
.ConfigureAwait(false);
306-
await ch.CloseAsync(cancellationToken)
307+
await channel.CloseAsync(cancellationToken)
307308
.ConfigureAwait(false);
308309
}
309310
}
@@ -351,11 +352,12 @@ private async Task RecoverQueuesAsync(IConnection connection,
351352
try
352353
{
353354
string newName = string.Empty;
354-
using (IChannel ch = await connection.CreateChannelAsync(cancellationToken: cancellationToken).ConfigureAwait(false))
355+
IChannel channel = await connection.CreateChannelAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
356+
await using (channel.ConfigureAwait(false))
355357
{
356-
newName = await recordedQueue.RecoverAsync(ch, cancellationToken)
358+
newName = await recordedQueue.RecoverAsync(channel, cancellationToken)
357359
.ConfigureAwait(false);
358-
await ch.CloseAsync(cancellationToken)
360+
await channel.CloseAsync(cancellationToken)
359361
.ConfigureAwait(false);
360362
}
361363
string oldName = recordedQueue.Name;
@@ -463,11 +465,12 @@ private async ValueTask RecoverBindingsAsync(IConnection connection,
463465
{
464466
try
465467
{
466-
using (IChannel ch = await connection.CreateChannelAsync(cancellationToken: cancellationToken).ConfigureAwait(false))
468+
IChannel channel = await connection.CreateChannelAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
469+
await using (channel.ConfigureAwait(false))
467470
{
468-
await binding.RecoverAsync(ch, cancellationToken)
471+
await binding.RecoverAsync(channel, cancellationToken)
469472
.ConfigureAwait(false);
470-
await ch.CloseAsync(cancellationToken)
473+
await channel.CloseAsync(cancellationToken)
471474
.ConfigureAwait(false);
472475
}
473476
}

projects/RabbitMQ.Client/Impl/AutorecoveringConnection.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,9 @@ await RecordChannelAsync(channel, channelsSemaphoreHeld: false, cancellationToke
264264
return channel;
265265
}
266266

267-
public void Dispose()
267+
public void Dispose() => DisposeAsync().AsTask().GetAwaiter().GetResult();
268+
269+
public async ValueTask DisposeAsync()
268270
{
269271
if (_disposed)
270272
{
@@ -273,7 +275,8 @@ public void Dispose()
273275

274276
try
275277
{
276-
_innerConnection.Dispose();
278+
await _innerConnection.DisposeAsync()
279+
.ConfigureAwait(false);
277280
}
278281
catch (OperationInterruptedException)
279282
{

projects/RabbitMQ.Client/Impl/ChannelBase.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,26 @@ protected virtual void Dispose(bool disposing)
568568
}
569569
}
570570

571+
public async ValueTask DisposeAsync()
572+
{
573+
await DisposeAsyncCore()
574+
.ConfigureAwait(false);
575+
576+
Dispose(false);
577+
}
578+
579+
protected virtual async ValueTask DisposeAsyncCore()
580+
{
581+
if (IsOpen)
582+
{
583+
await this.AbortAsync().ConfigureAwait(false);
584+
}
585+
586+
ConsumerDispatcher.Dispose();
587+
_rpcSemaphore.Dispose();
588+
_confirmSemaphore?.Dispose();
589+
}
590+
571591
public Task ConnectionTuneOkAsync(ushort channelMax, uint frameMax, ushort heartbeat, CancellationToken cancellationToken)
572592
{
573593
var method = new ConnectionTuneOk(channelMax, frameMax, heartbeat);

projects/RabbitMQ.Client/Impl/Connection.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,9 @@ internal ValueTask WriteAsync(RentedMemory frames, CancellationToken cancellatio
485485
return _frameHandler.WriteAsync(frames, cancellationToken);
486486
}
487487

488-
public void Dispose()
488+
public void Dispose() => DisposeAsync().AsTask().GetAwaiter().GetResult();
489+
490+
public async ValueTask DisposeAsync()
489491
{
490492
if (_disposed)
491493
{
@@ -496,7 +498,8 @@ public void Dispose()
496498
{
497499
if (IsOpen)
498500
{
499-
this.AbortAsync().GetAwaiter().GetResult();
501+
await this.AbortAsync()
502+
.ConfigureAwait(false);
500503
}
501504

502505
_session0.Dispose();

projects/Test/Applications/CreateChannel/CreateChannel.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
<PropertyGroup>
1616
<OutputType>Exe</OutputType>
17+
<LangVersion>9.0</LangVersion>
1718
</PropertyGroup>
1819

1920
<ItemGroup>

0 commit comments

Comments
 (0)