Skip to content

Commit

Permalink
support for async scope in DefaultAutoSubscriberMessageDispatcher (#1723
Browse files Browse the repository at this point in the history
)

follow-up for #1571

Co-authored-by: Wiebe Tijsma <wiebe@tijsma.com>
  • Loading branch information
OwnageIsMagic and zidad committed Mar 8, 2024
1 parent bd1bcf0 commit 2f1f885
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,22 @@ public DefaultAutoSubscriberMessageDispatcher() : this(new ActivatorBasedResolve
where TMessage : class
where TAsyncConsumer : class, IConsumeAsync<TMessage>
{
using var scope = resolver.CreateScope();
var asyncConsumer = scope.Resolve<TAsyncConsumer>();
await asyncConsumer.ConsumeAsync(message, cancellationToken).ConfigureAwait(false);
var scope = resolver.CreateScope();
try
{
var asyncConsumer = scope.Resolve<TAsyncConsumer>();
await asyncConsumer.ConsumeAsync(message, cancellationToken).ConfigureAwait(false);
}
finally
{
if (scope is IAsyncDisposable ad)
await ad.DisposeAsync().ConfigureAwait(false);
else
scope.Dispose();
}
}

private class ActivatorBasedResolver : IServiceResolver
private sealed class ActivatorBasedResolver : IServiceResolver
{
public TService Resolve<TService>() where TService : class => Activator.CreateInstance<TService>();

Expand Down

0 comments on commit 2f1f885

Please sign in to comment.