diff --git a/src/Core/src/Eventuous.Persistence/Diagnostics/Tracing/BaseTracer.cs b/src/Core/src/Eventuous.Persistence/Diagnostics/Tracing/BaseTracer.cs index d1a320a6a..8f899bc25 100644 --- a/src/Core/src/Eventuous.Persistence/Diagnostics/Tracing/BaseTracer.cs +++ b/src/Core/src/Eventuous.Persistence/Diagnostics/Tracing/BaseTracer.cs @@ -59,12 +59,12 @@ protected async IAsyncEnumerable TraceEnumerable( var enumerator = source.GetAsyncEnumerator(cancellationToken); - await using (enumerator.ConfigureAwait(false)) { + await using (enumerator.NoContext()) { while (true) { bool moved; try { - moved = await enumerator.MoveNextAsync().ConfigureAwait(false); + moved = await enumerator.MoveNextAsync().NoContext(); } catch (Exception e) { activity?.SetActivityStatus(ActivityStatus.Error(e)); measure.SetError(); diff --git a/src/Core/src/Eventuous.Persistence/EventStore/StoreFunctions.cs b/src/Core/src/Eventuous.Persistence/EventStore/StoreFunctions.cs index 18a5f430f..462bb4643 100644 --- a/src/Core/src/Eventuous.Persistence/EventStore/StoreFunctions.cs +++ b/src/Core/src/Eventuous.Persistence/EventStore/StoreFunctions.cs @@ -140,7 +140,7 @@ CancellationToken cancellationToken try { var result = new List(); - await foreach (var evt in eventReader.ReadEventsBackwards(stream, start, count, cancellationToken).ConfigureAwait(false)) { + await foreach (var evt in eventReader.ReadEventsBackwards(stream, start, count, cancellationToken).NoContext(cancellationToken)) { result.Add(evt); } @@ -218,24 +218,26 @@ [EnumeratorCancellation] CancellationToken cancellationToken var yielded = 0; long lastRevision = 0; - await using var enumerator = eventReader.ReadEvents(streamName, position, pageSize, cancellationToken).GetAsyncEnumerator(cancellationToken); + var enumerator = eventReader.ReadEvents(streamName, position, pageSize, cancellationToken).GetAsyncEnumerator(cancellationToken); - while (true) { - bool moved; + await using (enumerator.NoContext()) { + while (true) { + bool moved; - try { - moved = await enumerator.MoveNextAsync().NoContext(); - } catch (StreamNotFound) when (!failIfNotFound) { - yield break; - } + try { + moved = await enumerator.MoveNextAsync().NoContext(); + } catch (StreamNotFound) when (!failIfNotFound) { + yield break; + } - if (!moved) break; + if (!moved) break; - var evt = enumerator.Current; - yielded++; - lastRevision = evt.Revision; + var evt = enumerator.Current; + yielded++; + lastRevision = evt.Revision; - yield return evt; + yield return evt; + } } if (yielded < pageSize) yield break; diff --git a/src/Core/src/Eventuous.Shared/Tools/TaskExtensions.cs b/src/Core/src/Eventuous.Shared/Tools/TaskExtensions.cs index 67d5d3870..cb600ff6b 100644 --- a/src/Core/src/Eventuous.Shared/Tools/TaskExtensions.cs +++ b/src/Core/src/Eventuous.Shared/Tools/TaskExtensions.cs @@ -25,6 +25,9 @@ static class TaskExtensions { public static ConfiguredCancelableAsyncEnumerable NoContext(this IAsyncEnumerable source, CancellationToken cancellationToken) => source.WithCancellation(cancellationToken).ConfigureAwait(false); + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ConfiguredAsyncDisposable NoContext(this IAsyncDisposable disposable) => disposable.ConfigureAwait(false); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Task WhenAll(this IEnumerable tasks) => Task.WhenAll(tasks); diff --git a/src/Core/src/Eventuous.Subscriptions/Channels/ChannelExtensions.cs b/src/Core/src/Eventuous.Subscriptions/Channels/ChannelExtensions.cs index da029678f..b45c7ecf4 100644 --- a/src/Core/src/Eventuous.Subscriptions/Channels/ChannelExtensions.cs +++ b/src/Core/src/Eventuous.Subscriptions/Channels/ChannelExtensions.cs @@ -114,7 +114,7 @@ [EnumeratorCancellation] CancellationToken cancellationToken // Propagate possible failure of the channel. if (source.Completion.IsCompleted) - await source.Completion.ConfigureAwait(false); + await source.Completion.NoContext(); } finally { timerCts.Dispose(); } } } diff --git a/src/SqlServer/src/Eventuous.SqlServer/Projections/SqlServerProjector.cs b/src/SqlServer/src/Eventuous.SqlServer/Projections/SqlServerProjector.cs index e3362edd2..2e12439e5 100644 --- a/src/SqlServer/src/Eventuous.SqlServer/Projections/SqlServerProjector.cs +++ b/src/SqlServer/src/Eventuous.SqlServer/Projections/SqlServerProjector.cs @@ -34,10 +34,12 @@ protected void On(ProjectToSqlServerAsync handler) where T : class => base.On(async ctx => await Handle(ctx, handler).NoContext()); async Task Handle(MessageConsumeContext context, ProjectToSqlServerAsync handler) where T : class { - await using var connection = await ConnectionFactory.GetConnection(_connectionString, context.CancellationToken); + var connection = await ConnectionFactory.GetConnection(_connectionString, context.CancellationToken).NoContext(); - var cmd = await handler(connection, context).ConfigureAwait(false); - await cmd.ExecuteNonQueryAsync(context.CancellationToken).ConfigureAwait(false); + await using (connection.NoContext()) { + var cmd = await handler(connection, context).NoContext(); + await cmd.ExecuteNonQueryAsync(context.CancellationToken).NoContext(); + } } protected static SqlCommand Project(SqlConnection connection, string commandText, params SqlParameter[] parameters) { diff --git a/src/Sqlite/src/Eventuous.Sqlite/Projections/SqliteProjector.cs b/src/Sqlite/src/Eventuous.Sqlite/Projections/SqliteProjector.cs index aefc35430..18fe144d9 100644 --- a/src/Sqlite/src/Eventuous.Sqlite/Projections/SqliteProjector.cs +++ b/src/Sqlite/src/Eventuous.Sqlite/Projections/SqliteProjector.cs @@ -34,10 +34,12 @@ protected void On(ProjectToSqliteAsync handler) where T : class => base.On(async ctx => await Handle(ctx, handler).NoContext()); async Task Handle(MessageConsumeContext context, ProjectToSqliteAsync handler) where T : class { - await using var connection = await ConnectionFactory.GetConnection(_connectionString, context.CancellationToken); + var connection = await ConnectionFactory.GetConnection(_connectionString, context.CancellationToken).NoContext(); - var cmd = await handler(connection, context).ConfigureAwait(false); - await cmd.ExecuteNonQueryAsync(context.CancellationToken).ConfigureAwait(false); + await using (connection.NoContext()) { + var cmd = await handler(connection, context).NoContext(); + await cmd.ExecuteNonQueryAsync(context.CancellationToken).NoContext(); + } } protected static SqliteCommand Project(SqliteConnection connection, string commandText, params SqliteParameter[] parameters) {