Example #1
0
 public Task SubscribeToAllAsync(Func <IEvent, Task> handler, CancellationToken cancellationToken = default)
 {
     return(_messageBusSubscriber.SubscribeAsync(async envelope =>
     {
         using (CorrelationManager.NewCorrelationId(envelope.GetCorrelationId()))
         {
             await handler(envelope.Payload);
         }
     }, cancellationToken, _messagingTopicResolver.ResolveTopicName(), _subscriberOptions));
 }
        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());
        }
Example #3
0
        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);
        }
Example #4
0
 public Task <IDisposable> SubscribeAsync <TMessage>(Func <MessagingEnvelope <TMessage>, Task> handler,
                                                     MessagingSubscriberOptions options  = null,
                                                     CancellationToken cancellationToken = default)
 => _busSubscriber.SubscribeAsync(handler, options, cancellationToken);