/// <summary> /// Extends the visibility timeout of a message. /// </summary> /// <param name="message">The message.</param> /// <param name="timeout">The timeout to be extended.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>A TPL task.</returns> public virtual async Task ExtendTimeout(QueueMessage message, TimeSpan timeout, CancellationToken cancellationToken) { if (message == null) { throw new ArgumentNullException(nameof(message)); } if (string.IsNullOrWhiteSpace(message.MessageId)) { throw new ArgumentException("The message ID is required"); } try { CloudQueueMessage cloudQueueMessage = new CloudQueueMessage( message.MessageId, popReceipt: message.ImplementationData); await this.cloudQueue.UpdateMessageAsync( cloudQueueMessage, timeout, MessageUpdateFields.Visibility, cancellationToken); message.NextVisibleTime = cloudQueueMessage.NextVisibleTime.Value.UtcDateTime; message.ImplementationData = cloudQueueMessage.PopReceipt; } catch (StorageException ex) { if (ex.RequestInformation.HttpStatusCode == 404 && "MessageNotFound".Equals(ex.RequestInformation.ExtendedErrorInformation.ErrorCode, StringComparison.InvariantCulture)) { throw new MessageNotFoundException(); } throw; } }
/// <summary> /// Gets the next message from the queue /// </summary> /// <param name="visibilityTimeout">The amount of time the returned message will stay hidden until it becomes visible again</param> /// <param name="cancellationToken">The cancellation token</param> /// <returns>A queue message, or null of there's none available</returns> public virtual async Task<QueueMessage> GetMessage(TimeSpan visibilityTimeout, CancellationToken cancellationToken) { if (visibilityTimeout < TimeSpan.FromSeconds(1)) { throw new ArgumentException("Visibility timeout must be at least 1 second", nameof(visibilityTimeout)); } CloudQueueMessage cloudMessage = await this.cloudQueue.GetMessageAsync(visibilityTimeout, null /* options */, null /* operation context */, cancellationToken); if (cloudMessage == null) { return null; } QueueMessage msg = new QueueMessage { MessageId = cloudMessage.Id, Body = cloudMessage.AsString, DequeueCount = cloudMessage.DequeueCount, ImplementationData = cloudMessage.PopReceipt, }; if (cloudMessage.InsertionTime != null) { msg.InsertionTime = cloudMessage.InsertionTime.Value.UtcDateTime; } if (cloudMessage.ExpirationTime != null) { msg.ExpirationTime = cloudMessage.ExpirationTime.Value.UtcDateTime; } if (cloudMessage.NextVisibleTime != null) { msg.NextVisibleTime = cloudMessage.NextVisibleTime.Value.UtcDateTime; } return msg; }
/// <summary> /// Deletes a message from the queue /// </summary> /// <param name="message">The queue message</param> /// <param name="cancellationToken">The cancellation token</param> /// <returns>The deletion task</returns> public virtual async Task DeleteMessage(QueueMessage message, CancellationToken cancellationToken) { if (message == null) { throw new ArgumentNullException(nameof(message)); } if (string.IsNullOrWhiteSpace(message.MessageId)) { throw new ArgumentException("The message ID is required"); } try { await this.cloudQueue.DeleteMessageAsync( message.MessageId, popReceipt: message.ImplementationData, cancellationToken: cancellationToken); } catch (StorageException ex) { if (ex.RequestInformation.HttpStatusCode == 404 && "MessageNotFound".Equals(ex.RequestInformation.ExtendedErrorInformation.ErrorCode, StringComparison.InvariantCulture)) { throw new MessageNotFoundException(); } throw; } }