Пример #1
0
        public async Task DequeueAndUpdateAsync()
        {
            // Get a connection string to our Azure Storage account.
            string connectionString = ConnectionString;

            // Get a reference to a queue named "sample-queue" and then create it
            QueueClient queue = new QueueClient(connectionString, Randomize("sample-queue"));
            await queue.CreateAsync();

            try
            {
                // Add several messages to the queue
                await queue.EnqueueMessageAsync("first");

                await queue.EnqueueMessageAsync("second");

                await queue.EnqueueMessageAsync("third");

                // Get the messages from the queue with a short visibility timeout
                List <DequeuedMessage> messages = new List <DequeuedMessage>();
                foreach (DequeuedMessage message in (await queue.DequeueMessagesAsync(10, TimeSpan.FromSeconds(1))).Value)
                {
                    // Tell the service we need a little more time to process the message
                    UpdatedMessage changedMessage = await queue.UpdateMessageAsync(
                        message.MessageText,
                        message.MessageId,
                        message.PopReceipt,
                        TimeSpan.FromSeconds(5));

                    messages.Add(message.Update(changedMessage));
                }

                // Wait until the visibility window times out
                await Task.Delay(TimeSpan.FromSeconds(1.5));

                // Ensure the messages aren't visible yet
                Assert.AreEqual(0, (await queue.DequeueMessagesAsync(10)).Value.Count());

                // Finish processing the messages
                foreach (DequeuedMessage message in messages)
                {
                    // Tell the service we need a little more time to process the message
                    await queue.DeleteMessageAsync(message.MessageId, message.PopReceipt);
                }
            }
            finally
            {
                // Clean up after the test when we're finished
                await queue.DeleteAsync();
            }
        }
        public async Task MessageSample()
        {
            // Instantiate a new QueueServiceClient using a connection string.
            QueueServiceClient queueServiceClient = new QueueServiceClient(TestConfigurations.DefaultTargetTenant.ConnectionString);

            // Instantiate a new QueueClient
            QueueClient queueClient = queueServiceClient.GetQueueClient($"myqueue2-{Guid.NewGuid()}");

            try
            {
                // Create your new Queue in the service
                await queueClient.CreateAsync();

                // Instantiate a new MessagesClient
                // Enqueue a message to the queue
                Response <EnqueuedMessage> enqueueResponse = await queueClient.EnqueueMessageAsync("my message");

                // Peek message
                Response <IEnumerable <PeekedMessage> > peekResponse = await queueClient.PeekMessagesAsync();

                // Update message
                await queueClient.UpdateMessageAsync("new message", enqueueResponse.Value.MessageId, enqueueResponse.Value.PopReceipt);

                // Dequeue message
                Response <IEnumerable <DequeuedMessage> > dequeueResponse = await queueClient.DequeueMessagesAsync();

                // Delete Message
                await queueClient.DeleteMessageAsync(enqueueResponse.Value.MessageId, dequeueResponse.Value.First().PopReceipt);
            }
            finally
            {
                // Delete your Queue in the service
                await queueClient.DeleteAsync();
            }
        }
Пример #3
0
        public async Task DequeueAsync_Error()
        {
            // Arrange
            var queueName = GetNewQueueName();
            QueueServiceClient service = GetServiceClient_SharedKey();
            QueueClient        queue   = InstrumentClient(service.GetQueueClient(queueName));

            // Act
            await TestHelper.AssertExpectedExceptionAsync <StorageRequestFailedException>(
                queue.DequeueMessagesAsync(),
                actualException => Assert.AreEqual("QueueNotFound", actualException.ErrorCode));
        }
Пример #4
0
        public async Task DequeueAsync()
        {
            // Get a connection string to our Azure Storage account.
            string connectionString = ConnectionString;

            // Get a reference to a queue named "sample-queue" and then create it
            QueueClient queue = new QueueClient(connectionString, Randomize("sample-queue"));
            await queue.CreateAsync();

            try
            {
                // Add several messages to the queue
                await queue.EnqueueMessageAsync("first");

                await queue.EnqueueMessageAsync("second");

                await queue.EnqueueMessageAsync("third");

                await queue.EnqueueMessageAsync("fourth");

                await queue.EnqueueMessageAsync("fifth");

                // Get the messages from the queue
                List <string> messages = new List <string>();
                foreach (DequeuedMessage message in (await queue.DequeueMessagesAsync(maxMessages: 10)).Value)
                {
                    // "Process" the message
                    messages.Add(message.MessageText);

                    // Let the service know we finished with the message and
                    // it can be safely deleted.
                    await queue.DeleteMessageAsync(message.MessageId, message.PopReceipt);
                }

                // Verify the messages
                Assert.AreEqual(5, messages.Count);
                Assert.Contains("first", messages);
                Assert.Contains("second", messages);
                Assert.Contains("third", messages);
                Assert.Contains("fourth", messages);
                Assert.Contains("fifth", messages);
            }
            finally
            {
                // Clean up after the test when we're finished
                await queue.DeleteAsync();
            }
        }