Quickstart: Azure Queue Storage - .NET

Last updated: Publish date: Article ID: 7478140

This quickstart shows you how to use Azure Queue Storage from a .NET application. Azure Queue Storage is a service that allows you to store large numbers of messages that can be accessed from anywhere in the world. You can use queues to build distributed application components that run in the cloud or on-premises.

Prerequisites

Create a Storage Account

You can create a storage account using the Azure portal, Azure CLI, or Azure PowerShell. For this quickstart, we'll use the Azure portal.

  1. Go to the Azure portal.
  2. In the search bar, enter "Storage accounts" and select it from the list.
  3. Click "Create".
  4. Select your subscription and resource group, or create a new one.
  5. Enter a unique name for your storage account.
  6. Choose a region.
  7. Select "Standard" performance and "LRS" redundancy.
  8. Click "Review + create" and then "Create".

Get the Connection String

Once your storage account is created, navigate to it in the Azure portal. On the "Access keys" page, copy the Connection string.

Security Note: The connection string contains your account key and should be treated as sensitive information. Do not share it publicly.

Create a .NET Project

Open Visual Studio and create a new Console Application project using the .NET template.

Install the Azure Queue Storage Client Library

Use the NuGet Package Manager to install the Azure.Storage.Queues package:

dotnet add package Azure.Storage.Queues

Write Code to Interact with Queue Storage

Replace the contents of your Program.cs file with the following code. Make sure to replace YOUR_CONNECTION_STRING with your actual storage account connection string.


using Azure.Storage.Queues;
using System;
using System.Threading.Tasks;

public class Program
{
    // Replace with your actual storage account connection string
    private static string connectionString = "YOUR_CONNECTION_STRING";
    private static string queueName = "my-quickstart-queue";

    public static async Task Main(string[] args)
    {
        Console.WriteLine("Azure Queue Storage - .NET Quickstart");

        // Create a QueueClient object
        QueueClient queueClient = new QueueClient(connectionString, queueName);

        // Create the queue if it doesn't exist
        await queueClient.CreateIfNotExistsAsync();
        Console.WriteLine($"Queue '{queueName}' created or already exists.");

        // Send a message to the queue
        await queueClient.SendMessageAsync("Hello, Azure Queue Storage!");
        Console.WriteLine("Message sent to the queue.");

        // Peek at the message in the queue
        var peekedMessage = await queueClient.PeekMessageAsync();
        if (peekedMessage.HasValue)
        {
            Console.WriteLine($"Peeked message: {peekedMessage.Value.MessageText}");
        }

        // Receive and delete the message from the queue
        var receivedMessage = await queueClient.ReceiveMessageAsync();
        if (receivedMessage.HasValue)
        {
            Console.WriteLine($"Received message: {receivedMessage.Value.MessageText}");
            await queueClient.DeleteMessageAsync(receivedMessage.Value.MessageId, receivedMessage.Value.PopReceipt);
            Console.WriteLine("Message received and deleted.");
        }

        Console.WriteLine("\nPress any key to exit.");
        Console.ReadKey();
    }
}
            

Run the Application

Build and run your .NET application:

dotnet run

You should see output indicating that the queue was created, a message was sent, peeked at, and then received and deleted.

Clean up Resources

To avoid incurring ongoing charges, you can delete the storage account you created for this quickstart.

  1. In the Azure portal, navigate to your storage account.
  2. Click "Delete".
  3. Confirm the deletion.

Next Steps

You've successfully used Azure Queue Storage in a .NET application. Here are some next steps:

View Full Code Sample