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
- An Azure subscription. If you don't have one, create a free account before you begin.
- A storage account. For this quickstart, we'll create a new one.
- Visual Studio 2022.
- .NET SDK.
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.
- Go to the Azure portal.
- In the search bar, enter "Storage accounts" and select it from the list.
- Click "Create".
- Select your subscription and resource group, or create a new one.
- Enter a unique name for your storage account.
- Choose a region.
- Select "Standard" performance and "LRS" redundancy.
- 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.
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.
- In the Azure portal, navigate to your storage account.
- Click "Delete".
- Confirm the deletion.
Next Steps
You've successfully used Azure Queue Storage in a .NET application. Here are some next steps:
- Learn more about Azure Queue Storage features.
- Explore the client library documentation for more advanced scenarios.
- Integrate queues with other Azure services like Azure Functions.