Esempio n. 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();
            }
        }
Esempio n. 2
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();
            }
        }
Esempio n. 3
0
        public async Task EnqueueAsync()
        {
            // Get a connection string to our Azure Storage account.  You can
            // obtain your connection string from the Azure Portal (click
            // Access Keys under Settings in the Portal Storage account blade)
            // or using the Azure CLI with:
            //
            //     az storage account show-connection-string --name <account_name> --resource-group <resource_group>
            //
            // And you can provide the connection string to your application
            // using an environment variable.
            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 a message to our queue
                await queue.EnqueueMessageAsync("Hello, Azure!");

                // Verify we uploaded one message
                Assert.AreEqual(1, (await queue.PeekMessagesAsync(10)).Value.Count());
            }
            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();
            }
        }
        public static async Task TestWithQueue(string accountName, string queueName, string message)
        {
            var uri   = new Uri($"https://{accountName}.queue.core.windows.net/{queueName}");
            var queue = new QueueClient(uri, new DefaultAzureCredential());

            Console.WriteLine($@"calling CreateAsync() on ""{uri}""");
            await queue.CreateAsync();

            Console.WriteLine($@"calling EnqueueMessageAsync(""{message}"") on ""{uri}""");
            await queue.EnqueueMessageAsync(message);
        }
Esempio n. 6
0
        public async Task EnqueueAsync_Error()
        {
            // Arrange
            var queueName = GetNewQueueName();
            QueueServiceClient service = GetServiceClient_SharedKey();
            QueueClient        queue   = InstrumentClient(service.GetQueueClient(queueName));

            // Act
            await TestHelper.AssertExpectedExceptionAsync <StorageRequestFailedException>(
                queue.EnqueueMessageAsync(string.Empty),
                actualException => Assert.AreEqual("QueueNotFound", actualException.ErrorCode));
        }