Пример #1
0
        public async IAsyncEnumerable <T> ReceiveMessages <T>(
            string queueOrTopicName,
            string subscriptionName = default,
            ServiceBusSubscriberReceiveOptions options = default,
            CancellationToken cancellationToken        = default)
            where T : class
        {
            var receiveMessageType = options?.ReceiveMessageType ?? ServiceBusSubscriberReceiveMessageTypes.Payload;

            await using var receiver = await GetServiceBusReceiver(queueOrTopicName, subscriptionName, options, cancellationToken);

            await foreach (var serviceBusMessage in receiver.ReceiveMessagesAsync(cancellationToken))
            {
                var payload = GetPayload <T>(serviceBusMessage, receiveMessageType, cancellationToken);
                await receiver.CompleteMessageAsync(serviceBusMessage, cancellationToken);

                yield return(payload);
            }
        }
Пример #2
0
        private async Task <ServiceBusReceiver> GetServiceBusReceiver(
            string queueOrTopicName,
            string subscriptionName = default,
            ServiceBusSubscriberReceiveOptions options = default,
            CancellationToken cancellationToken        = default)
        {
            var(sessionId, _, connectToDeadLetterQueue) = (options?.SessionId, ServiceBusSubscriberReceiveMessageTypes.Payload, options?.ConnectToDeadLetterQueue ?? false);

            if (string.IsNullOrWhiteSpace(subscriptionName))
            {
                if (string.IsNullOrWhiteSpace(sessionId))
                {
                    return(_client.CreateReceiver(
                               queueOrTopicName,
                               new ServiceBusReceiverOptions
                    {
                        SubQueue = connectToDeadLetterQueue ? SubQueue.DeadLetter : SubQueue.None
                    }));
                }

                return(await _client.AcceptSessionAsync(
                           queueOrTopicName,
                           sessionId,
                           cancellationToken : cancellationToken));
            }

            if (string.IsNullOrWhiteSpace(sessionId))
            {
                return(_client.CreateReceiver(
                           queueOrTopicName,
                           subscriptionName,
                           new ServiceBusReceiverOptions
                {
                    SubQueue = connectToDeadLetterQueue ? SubQueue.DeadLetter : SubQueue.None
                }));
            }

            return(await _client.AcceptSessionAsync(
                       queueOrTopicName,
                       subscriptionName,
                       sessionId,
                       cancellationToken : cancellationToken));
        }
Пример #3
0
        public async Task StartReceiveMessages(
            string queueOrTopicName,
            string subscriptionName = default,
            Func <string, string, object, Task> processMessageFunc  = default,
            Func <string, string, Exception, Task> processErrorFunc = default,
            ServiceBusSubscriberReceiveOptions options = default,
            CancellationToken cancellationToken        = default)
        {
            if (_serviceBusSubscriberProcessor != null)
            {
                _logger.LogError("Service Bus processor already initialized. Stop it first");
                throw new ServiceBusSubscriberOperationException(
                          "Service Bus processor already initialized. Stop it first.");
            }

            try
            {
                _serviceBusSubscriberProcessor =
                    new ServiceBusSubscriberProcessor(_client, queueOrTopicName, subscriptionName, options)
                {
                    QueueOrTopicName         = queueOrTopicName,
                    SubscriptionName         = subscriptionName,
                    ClientProcessMessageFunc = processMessageFunc,
                    ClientProcessErrorFunc   = processErrorFunc
                };

                _serviceBusSubscriberProcessor.ProcessMessageAsync += ProcessMessageInternal;
                _serviceBusSubscriberProcessor.ProcessErrorAsync   += ProcessErrorInternal;

                await _serviceBusSubscriberProcessor.StartProcessingAsync(cancellationToken);
            }
            catch (Exception ex)
            {
                await StopReceiveMessagesInternal(cancellationToken);

                _logger.LogError(ex, "Service Bus start receive messages from topic subscription failure");

                throw new ServiceBusSubscriberOperationException(
                          "Service Bus start receive messages from topic subscription failure", ex);
            }
        }
Пример #4
0
        public async Task <object> ReceiveMessage(
            string queueOrTopicName,
            string subscriptionName = default,
            ServiceBusSubscriberReceiveOptions options = default,
            CancellationToken cancellationToken        = default)
        {
            await using var receiver = await GetServiceBusReceiver(queueOrTopicName, subscriptionName, options, cancellationToken);

            var serviceBusMessage = await receiver.ReceiveMessageAsync(cancellationToken : cancellationToken);

            if (serviceBusMessage == null)
            {
                return(null);
            }

            var receiveMessageType = options?.ReceiveMessageType ?? ServiceBusSubscriberReceiveMessageTypes.Payload;
            var payload            = GetPayload(serviceBusMessage, receiveMessageType, cancellationToken);
            await receiver.CompleteMessageAsync(serviceBusMessage, cancellationToken);

            return(payload);
        }
Пример #5
0
        public ServiceBusSubscriberProcessor(ServiceBusClient client,
                                             string queueOrTopicName,
                                             string subscriptionName = default,
                                             ServiceBusSubscriberReceiveOptions options = default)
        {
            QueueOrTopicName = queueOrTopicName;
            SubscriptionName = subscriptionName;
            ServiceBusSubscriberReceiveOptions = options ?? new ServiceBusSubscriberReceiveOptions();

            var(sessionId, _, connectToDeadLetterQueue) = (options?.SessionId, ServiceBusSubscriberReceiveMessageTypes.Payload, options?.ConnectToDeadLetterQueue ?? false);

            if (string.IsNullOrWhiteSpace(subscriptionName))
            {
                if (string.IsNullOrWhiteSpace(sessionId))
                {
                    _serviceBusProcessorAsObject = client.CreateProcessor(
                        queueOrTopicName,
                        new ServiceBusProcessorOptions
                    {
                        AutoCompleteMessages = false,
                        SubQueue             = connectToDeadLetterQueue ? SubQueue.DeadLetter : SubQueue.None
                    });

                    return;
                }

                _serviceBusProcessorAsObject = client.CreateSessionProcessor(
                    queueOrTopicName,
                    new ServiceBusSessionProcessorOptions
                {
                    AutoCompleteMessages         = false,
                    MaxConcurrentSessions        = 5,
                    MaxConcurrentCallsPerSession = 2,
                    SessionIds = { sessionId }
                });

                return;
            }

            if (string.IsNullOrWhiteSpace(sessionId))
            {
                _serviceBusProcessorAsObject = client.CreateProcessor(
                    queueOrTopicName,
                    subscriptionName,
                    new ServiceBusProcessorOptions
                {
                    AutoCompleteMessages = false,
                    SubQueue             = connectToDeadLetterQueue ? SubQueue.DeadLetter : SubQueue.None
                });

                return;
            }

            _serviceBusProcessorAsObject = client.CreateSessionProcessor(
                queueOrTopicName,
                subscriptionName,
                new ServiceBusSessionProcessorOptions
            {
                AutoCompleteMessages         = false,
                MaxConcurrentSessions        = 5,
                MaxConcurrentCallsPerSession = 2,
                SessionIds = { sessionId }
            });
        }