protected override async Task ExecuteAsync(CancellationToken cancellationToken = default) { _logger.LogInformation("MessageBusSubscriberService for message type {MessageType} is starting", typeof(TMessage).GetPrettyName()); Task HandleMsg(MessagingEnvelope <TMessage> msg) => Handle(msg, cancellationToken); await _messageBusSubscriber.SubscribeAsync(HandleMsg, cancellationToken, null, _subscriberOptions); await cancellationToken.WhenCanceled(); await _messageBusSubscriber.UnSubscribeAsync(HandleMsg, CancellationToken.None); _logger.LogInformation("MessageBusSubscriberService for message type {MessageType} is stopping", typeof(TMessage).GetPrettyName()); }
public static async Task <MessagingEnvelope <TMessage> > WaitForMessage <TMessage>(this IMessageBusSubscriber <TMessage> subscriber, Func <MessagingEnvelope <TMessage>, bool> predicate, CancellationToken cancellationToken = default) { var tcs = new TaskCompletionSource <MessagingEnvelope <TMessage> >(); async Task HandleMessage(MessagingEnvelope <TMessage> msg) { if (predicate(msg)) { tcs.SetResult(msg); await subscriber.UnSubscribeAsync(HandleMessage, cancellationToken); } } await subscriber.SubscribeAsync(HandleMessage, cancellationToken); return(await tcs.Task); }