/// <summary>
        /// Receives a batch of <see cref="ServiceBusReceivedMessage" /> from the entity using <see cref="ReceiveMode"/> mode. <see cref="ReceiveMode"/> defaults to PeekLock mode.
        /// This method doesn't guarantee to return exact `maxMessages` messages, even if there are `maxMessages` messages available in the queue or topic.
        /// </summary>
        ///
        /// <param name="maxMessages">The maximum number of messages that will be received.</param>
        /// <param name="maxWaitTime">An optional <see cref="TimeSpan"/> specifying the maximum time to wait for the first message before returning an empty list if no messages have been received.
        /// If not specified, the <see cref="ServiceBusRetryOptions.TryTimeout"/> will be used.</param>
        /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request to cancel the operation.</param>
        ///
        /// <returns>List of messages received. Returns an empty list if no message is found.</returns>
        public virtual async Task <IList <ServiceBusReceivedMessage> > ReceiveBatchAsync(
            int maxMessages,
            TimeSpan?maxWaitTime = default,
            CancellationToken cancellationToken = default)
        {
            Argument.AssertAtLeast(maxMessages, 1, nameof(maxMessages));
            Argument.AssertNotClosed(IsDisposed, nameof(ServiceBusReceiver));
            cancellationToken.ThrowIfCancellationRequested <TaskCanceledException>();
            ServiceBusEventSource.Log.ReceiveMessageStart(Identifier, maxMessages);
            IList <ServiceBusReceivedMessage> messages = null;

            try
            {
                messages = await InnerReceiver.ReceiveBatchAsync(
                    maxMessages,
                    maxWaitTime,
                    cancellationToken).ConfigureAwait(false);
            }
            catch (Exception exception)
            {
                ServiceBusEventSource.Log.ReceiveMessageException(Identifier, exception);
                throw;
            }

            cancellationToken.ThrowIfCancellationRequested <TaskCanceledException>();
            ServiceBusEventSource.Log.ReceiveMessageComplete(Identifier, maxMessages);
            return(messages);
        }
        /// <summary>
        ///  Receives a batch of <see cref="ServiceBusMessage" /> from the entity using <see cref="ReceiveMode"/> mode.
        /// </summary>
        /// <param name="maxMessages">The maximum number of messages that will be received.</param>
        /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request to cancel the operation.</param>
        /// <returns></returns>
        public virtual async Task <IList <ServiceBusReceivedMessage> > ReceiveBatchAsync(
            int maxMessages,
            CancellationToken cancellationToken = default)
        {
            Argument.AssertNotClosed(IsClosed, nameof(ServiceBusReceiver));
            cancellationToken.ThrowIfCancellationRequested <TaskCanceledException>();

            try
            {
                return(await _innerReceiver.ReceiveBatchAsync(maxMessages, cancellationToken).ConfigureAwait(false));
            }
            finally
            {
                // TODO: Add log - SeviceBusEventSource.Log.ReceiveBatchAsyncComplete();
            }
        }
Exemple #3
0
        /// <summary>
        /// Receives a batch of <see cref="ServiceBusReceivedMessage" /> from the entity using <see cref="ReceiveMode"/> mode. <see cref="ReceiveMode"/> defaults to PeekLock mode.
        /// This method doesn't guarantee to return exact `maxMessages` messages, even if there are `maxMessages` messages available in the queue or topic.
        /// </summary>
        ///
        /// <param name="maxMessages">The maximum number of messages that will be received.</param>
        /// <param name="maxWaitTime">An optional <see cref="TimeSpan"/> specifying the maximum time to wait for the first message before returning an empty list if no messages have been received.
        /// If not specified, the <see cref="ServiceBusRetryOptions.TryTimeout"/> will be used.</param>
        /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request to cancel the operation.</param>
        ///
        /// <returns>List of messages received. Returns an empty list if no message is found.</returns>
        public virtual async Task <IList <ServiceBusReceivedMessage> > ReceiveBatchAsync(
            int maxMessages,
            TimeSpan?maxWaitTime = default,
            CancellationToken cancellationToken = default)
        {
            Argument.AssertAtLeast(maxMessages, 1, nameof(maxMessages));
            Argument.AssertNotClosed(IsDisposed, nameof(ServiceBusReceiver));
            if (maxWaitTime.HasValue)
            {
                Argument.AssertPositive(maxWaitTime.Value, nameof(maxWaitTime));
            }

            cancellationToken.ThrowIfCancellationRequested <TaskCanceledException>();
            Logger.ReceiveMessageStart(Identifier, maxMessages);
            using DiagnosticScope scope = _scopeFactory.CreateScope(
                      DiagnosticProperty.ReceiveActivityName,
                      requestedMessageCount: maxMessages);
            scope.Start();

            IList <ServiceBusReceivedMessage> messages = null;

            try
            {
                messages = await InnerReceiver.ReceiveBatchAsync(
                    maxMessages,
                    maxWaitTime,
                    cancellationToken).ConfigureAwait(false);
            }
            catch (Exception exception)
            {
                Logger.ReceiveMessageException(Identifier, exception.ToString());
                scope.Failed(exception);
                throw;
            }

            Logger.ReceiveMessageComplete(Identifier, messages.Count);
            scope.SetMessageData(messages);

            return(messages);
        }