/// <summary> /// Receives a <see cref="IList{ServiceBusReceivedMessage}"/> of deferred messages identified by <paramref name="sequenceNumbers"/>. /// </summary> /// /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request to cancel the operation.</param> /// <param name="sequenceNumbers">An <see cref="IEnumerable{T}"/> containing the sequence numbers to receive.</param> /// /// <returns>Messages identified by sequence number are returned. Returns null if no messages are found. /// Throws if the messages have not been deferred.</returns> /// <seealso cref="DeferMessageAsync(ServiceBusReceivedMessage, IDictionary{string, object}, CancellationToken)"/> /// <seealso cref="DeferMessageAsync(string, IDictionary{string, object}, CancellationToken)"/> public virtual async Task <IList <ServiceBusReceivedMessage> > ReceiveDeferredMessagesAsync( IEnumerable <long> sequenceNumbers, CancellationToken cancellationToken = default) { Argument.AssertNotClosed(IsDisposed, nameof(ServiceBusReceiver)); Argument.AssertNotNullOrEmpty(sequenceNumbers, nameof(sequenceNumbers)); cancellationToken.ThrowIfCancellationRequested <TaskCanceledException>(); var sequenceNumbersList = sequenceNumbers.ToList(); Logger.ReceiveDeferredMessageStart(Identifier, sequenceNumbersList); using DiagnosticScope scope = ScopeFactory.CreateScope(DiagnosticProperty.ReceiveDeferredActivityName); scope.AddAttribute( DiagnosticProperty.SequenceNumbersAttribute, string.Join(",", sequenceNumbers)); scope.Start(); IList <ServiceBusReceivedMessage> deferredMessages = null; try { deferredMessages = await InnerReceiver.ReceiveDeferredMessagesAsync( sequenceNumbersList, cancellationToken).ConfigureAwait(false); } catch (Exception exception) { Logger.ReceiveDeferredMessageException(Identifier, exception.ToString()); scope.Failed(exception); throw; } Logger.ReceiveDeferredMessageComplete(Identifier, deferredMessages.Count); scope.SetMessageData(deferredMessages); return(deferredMessages); }
/// <summary> /// Fetches a list of active messages without changing the state of the receiver or the message source. /// </summary> /// <param name="sequenceNumber">The sequence number from where to peek the message.</param> /// <param name="maxMessages">The maximum number of messages that will be fetched.</param> /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request to cancel the operation.</param> /// <returns>An <see cref="IList{ServiceBusReceivedMessage}" /> of messages that were peeked.</returns> private async Task <IList <ServiceBusReceivedMessage> > PeekMessagesInternalAsync( long?sequenceNumber, int maxMessages, CancellationToken cancellationToken) { Argument.AssertNotClosed(IsDisposed, nameof(ServiceBusReceiver)); cancellationToken.ThrowIfCancellationRequested <TaskCanceledException>(); Logger.PeekMessageStart(Identifier, sequenceNumber, maxMessages); using DiagnosticScope scope = ScopeFactory.CreateScope( DiagnosticProperty.PeekActivityName, requestedMessageCount: maxMessages); scope.Start(); IList <ServiceBusReceivedMessage> messages = new List <ServiceBusReceivedMessage>(); try { messages = await InnerReceiver.PeekMessagesAsync( sequenceNumber, maxMessages, cancellationToken) .ConfigureAwait(false); } catch (Exception exception) { Logger.PeekMessageException(Identifier, exception.ToString()); scope.Failed(exception); throw; } Logger.PeekMessageComplete(Identifier, messages.Count); scope.SetMessageData(messages); return(messages); }
private DiagnosticScope CreateDiagnosticScope(IEnumerable <ServiceBusMessage> messages, string activityName) { InstrumentMessages(messages); // create a new scope for the specified operation DiagnosticScope scope = _scopeFactory.CreateScope( activityName, DiagnosticProperty.ClientKind); scope.SetMessageData(messages); return(scope); }
protected async Task ProcessOneMessageWithinScopeAsync(ServiceBusReceivedMessage message, string activityName, CancellationToken cancellationToken) { using DiagnosticScope scope = _scopeFactory.CreateScope(activityName); scope.Start(); scope.SetMessageData(new ServiceBusReceivedMessage[] { message }); try { await ProcessOneMessage( message, cancellationToken) .ConfigureAwait(false); } catch (Exception ex) { scope.Failed(ex); throw; } }
/// <summary> /// Receives a list 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 are available. /// 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> > ReceiveMessagesAsync( 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.ReceiveMessagesAsync( maxMessages, maxWaitTime, cancellationToken).ConfigureAwait(false); await ApplyPlugins(messages).ConfigureAwait(false); } catch (Exception exception) { Logger.ReceiveMessageException(Identifier, exception.ToString()); scope.Failed(exception); throw; } Logger.ReceiveMessageComplete(Identifier, messages.Count); scope.SetMessageData(messages); return(messages); }