通过 .NET 开始使用 Azure 队列存储Get started with Azure Queue storage using .NET

概述Overview

Azure 队列存储用于在应用程序组件之间进行云消息传送。Azure Queue storage provides cloud messaging between application components. 设计可缩放的应用程序时,应用程序组件通常是分离的,各组件可以独立缩放。In designing applications for scale, application components are often decoupled so they can scale independently. 队列存储在应用程序组件之间提供异步消息传送,无论这些组件是运行在云中、桌面上、本地服务器上还是移动设备上。Queue storage delivers asynchronous messaging between application components, whether they are running in the cloud, on the desktop, on an on-premises server, or on a mobile device. 队列存储还支持管理异步任务以及构建过程工作流。Queue storage also supports managing asynchronous tasks and building process work flows.

关于本教程About this tutorial

本教程演示如何针对使用 Azure 队列存储一些常见情形编写 .NET 代码。This tutorial shows how to write .NET code for some common scenarios using Azure Queue storage. 涉及的方案包括创建和删除队列、添加、读取和删除队列消息。Scenarios covered include creating and deleting queues and adding, reading, and deleting queue messages.

估计完成时间: 45 分钟Estimated time to complete: 45 minutes

先决条件Prerequisites

什么是队列存储?What is Queue storage?

Azure 队列存储是一项可存储大量消息的服务,用户可以通过经验证的呼叫,使用 HTTP 或 HTTPS 从世界任何地方访问这些消息。Azure Queue storage is a service for storing large numbers of messages that can be accessed from anywhere in the world via authenticated calls using HTTP or HTTPS. 一条队列消息的大小最多可为 64 KB,一个队列中可以包含数百万条消息,直至达到存储帐户的总容量限值。A single queue message can be up to 64 KB in size, and a queue can contain millions of messages, up to the total capacity limit of a storage account. 队列存储通常用于创建要异步处理的积压工作 (backlog)。Queue storage is often used to create a backlog of work to process asynchronously.

队列服务概念Queue service concepts

Azure 队列服务包含以下组件:The Azure Queue service contains the following components:

Azure 队列服务组件

  • 存储帐户: 对 Azure 存储进行的所有访问都要通过存储帐户完成。Storage Account: All access to Azure Storage is done through a storage account. 有关存储帐户的详细信息,请参阅存储帐户概述For more information about storage accounts, see Storage account overview.

  • 队列: 一个队列包含一组消息。Queue: A queue contains a set of messages. 所有消息必须位于相应的队列中。All messages must be in a queue. 请注意,队列名称必须全部小写。Note that the queue name must be all lowercase. 有关命名队列的信息,请参阅 命名队列和元数据For information on naming queues, see Naming Queues and Metadata.

  • 消息: 一条消息(无论哪种格式)的最大大小为 64 KB。Message: A message, in any format, of up to 64 KB. 消息可以保留在队列中的最长时间为 7 天。The maximum time that a message can remain in the queue is 7 days. 在 2017-07-29 或更高版本中,最大生存时间可以是任何正数,或者是 -1(表示消息不会过期)。For version 2017-07-29 or later, the maximum time-to-live can be any positive number, or -1 indicating that the message doesn't expire. 如果省略此参数,则默认的生存时间为 7 天。If this parameter is omitted, the default time-to-live is seven days.

  • URL 格式: 使用以下 URL 格式对队列进行寻址: http://<storage account>.queue.core.chinacloudapi.cn/<queue>URL format: Queues are addressable using the following URL format: http://<storage account>.queue.core.chinacloudapi.cn/<queue>

    可使用以下 URL 访问示意图中的某个队列:The following URL addresses a queue in the diagram:

    http://myaccount.queue.core.chinacloudapi.cn/incoming-orders

创建 Azure 存储帐户Create an Azure storage account

创建第一个 Azure 存储帐户的最简单方法是使用 Azure 门户The easiest way to create your first Azure storage account is by using the Azure portal. 若要了解更多信息,请参阅 创建存储帐户To learn more, see Create a storage account.

还可使用 Azure PowerShellAzure CLI适用于 .NET 的 Azure 存储资源提供程序创建 Azure 存储帐户。You can also create an Azure storage account by using Azure PowerShell, Azure CLI, or the Azure Storage Resource Provider for .NET.

如果暂时不想在 Azure 中创建存储帐户,也可以使用 Azurite 存储模拟器在本地环境中运行和测试代码。If you prefer not to create a storage account in Azure at this time, you can also use the Azurite storage emulator to run and test your code in a local environment. 有关详细信息,请参阅使用 Azurite 模拟器进行本地 Azure 存储开发For more information, see Use the Azurite emulator for local Azure Storage development.

设置开发环境Set up your development environment

接下来在 Visual Studio 中设置开发环境,即可试用本指南中的代码示例。Next, set up your development environment in Visual Studio so you're ready to try the code examples in this guide.

创建 Windows 控制台应用程序项目Create a Windows console application project

在 Visual Studio 中创建新的 Windows 控制台应用程序。In Visual Studio, create a new Windows console application. 以下步骤演示了如何在 Visual Studio 2019 中创建控制台应用程序。The following steps show you how to create a console application in Visual Studio 2019. 在其他版本的 Visual Studio 中,这些步骤是类似的。The steps are similar in other versions of Visual Studio.

  1. 选择“文件” > “新建” > “项目” Select File > New > Project
  2. 选择“平台” > “Windows”Select Platform > Windows
  3. 选择“控制台应用(.NET Framework)”Select Console App (.NET Framework)
  4. 选择“下一步”Select Next
  5. 在“项目名称”字段中输入应用程序的名称In the Project name field, enter a name for your application
  6. 选择“创建”Select Create

本教程中的所有代码示例都可以添加到控制台应用程序的 Program.cs 文件的 Main() 方法。All code examples in this tutorial can be added to the Main() method of your console application's Program.cs file.

可以在任意类型的 .NET 应用程序(包括 Azure 云服务或 Web 应用,以及桌面和移动应用程序)中使用 Azure 存储客户端库。You can use the Azure Storage client libraries in any type of .NET application, including an Azure cloud service or web app, and desktop and mobile applications. 为简单起见,我们在本指南中使用控制台应用程序。In this guide, we use a console application for simplicity.

使用 NuGet 安装所需包Use NuGet to install the required packages

为完成此教程,需要在项目中引用下述四个包:You need to reference the following four packages in your project to complete this tutorial:

可以使用 NuGet 获取这些包。You can use NuGet to obtain these packages. 执行以下步骤:Follow these steps:

  1. 在“解决方案资源管理器”中,右键单击项目并选择“管理 NuGet 包”。Right-click your project in Solution Explorer, and choose Manage NuGet Packages.
  2. 选择“浏览”Select Browse
  3. 在线搜索“Azure.Storage.Queues”,选择“安装”以安装存储客户端库及其依赖项。Search online for "Azure.Storage.Queues", and select Install to install the Storage client library and its dependencies. 这还会安装 Azure.Storage.Common 和 Azure.Core 库,它们是队列库的依赖项。This will also install the Azure.Storage.Common and Azure.Core libraries, which are dependencies of the queue library.
  4. 在线搜索“System.Configuration.ConfigurationManager”,选择“安装”以安装 Configuration Manager。Search online for "System.Configuration.ConfigurationManager", and select Install to install the Configuration Manager.

备注

用于 .NET 的 Azure SDK中也包含存储客户端库包。The Storage client libraries packages are also included in the Azure SDK for .NET. 但是我们建议同时从 NuGet 安装存储客户端库,以确保始终使用最新版本。However, we recommend that you also install the Storage client libraries from NuGet to ensure that you always have the latest versions.

适用于 .NET 的存储客户端库中的 ODataLib 依赖项通过 NuGet(而非 WCF 数据服务)上提供的 ODataLib 包来解析。The ODataLib dependencies in the Storage client libraries for .NET are resolved by the ODataLib packages available on NuGet, not from WCF Data Services. ODataLib 库可直接下载或者通过 NuGet 由代码项目引用。The ODataLib libraries can be downloaded directly, or referenced by your code project through NuGet. 存储空间客户端库使用的具体 ODataLib 包是 ODataEdmSpatialThe specific ODataLib packages used by the Storage client libraries are OData, Edm, and Spatial. 尽管这些库由 Azure 表存储类使用,但是用存储空间客户端库进行编程时,它们是必需的依赖项。While these libraries are used by the Azure Table storage classes, they are required dependencies for programming with the Storage client libraries.

确定目标环境Determine your target environment

可从两个环境中选择用于运行本指南中示例的环境:You have two environment options for running the examples in this guide:

  • 可针对云中的 Azure 存储帐户运行代码。You can run your code against an Azure Storage account in the cloud.
  • 可针对 Azurite 存储仿真器运行代码。You can run your code against the Azurite storage emulator. Azurite 是仿真云中 Azure 存储帐户的本地环境。Azurite is a local environment that emulates an Azure Storage account in the cloud. 当应用程序正在开发时,Azurite 是用于测试和调试代码的免费选项。Azurite is a free option for testing and debugging your code while your application is under development. 模拟器使用已知帐户和密钥。The emulator uses a well-known account and key. 有关详细信息,请参阅使用 Azurite 仿真器进行本地 Azure 存储开发和测试For more information, see Use the Azurite emulator for local Azure Storage development and testing.

备注

可以指向存储模拟器以避免引发与 Azure 存储有关的任何费用。You can target the storage emulator to avoid incurring any costs associated with Azure Storage. 但是,如果您确实选择指向云中的 Azure 存储帐户,则执行此教程的费用会忽略不计。However, if you do choose to target an Azure storage account in the cloud, costs for performing this tutorial will be negligible.

获取存储连接字符串Get your storage connection string

用于 .NET 的 Azure 存储客户端库支持使用存储连接字符串来配置终结点和用于访问存储服务的凭据。The Azure Storage client libraries for .NET support using a storage connection string to configure endpoints and credentials for accessing storage services. 有关详细信息,请参阅管理存储帐户访问密钥For more information, see Manage storage account access keys.

从 Azure 门户复制凭据Copy your credentials from the Azure portal

此示例代码需要对存储帐户访问进行授权。The sample code needs to authorize access to your storage account. 若要授权,请以连接字符串的形式向应用程序提供存储帐户凭据。To authorize, you provide the application with your storage account credentials in the form of a connection string. 若要查看存储帐户凭据,请执行以下操作:To view your storage account credentials:

  1. 导航到 Azure 门户Navigate to the Azure portal.

  2. 找到自己的存储帐户。Locate your storage account.

  3. 在存储帐户概述的“设置”部分,选择“访问密钥”。 In the Settings section of the storage account overview, select Access keys. 此时会显示帐户访问密钥,以及每个密钥的完整连接字符串。Your account access keys appear, as well as the complete connection string for each key.

  4. 找到“密钥 1”下面的“连接字符串”值,单击“复制”按钮复制该连接字符串。 Find the Connection string value under key1, and click the Copy button to copy the connection string. 下一步需将此连接字符串值添加到某个环境变量。You will add the connection string value to an environment variable in the next step.

    显示如何从 Azure 门户复制连接字符串的屏幕截图

有关连接字符串的详细信息,请参阅配置 Azure 存储的连接字符串For more information about connection strings, see Configure a connection string to Azure Storage.

备注

存储帐户密钥类似于存储帐户的根密码。Your storage account key is similar to the root password for your storage account. 始终要小心保护存储帐户密钥。Always be careful to protect your storage account key. 避免将其分发给其他用户、对其进行硬编码或将其保存在其他人可以访问的纯文本文件中。Avoid distributing it to other users, hard-coding it, or saving it in a plain-text file that is accessible to others. 如果认为密钥可能已泄漏,请使用 Azure 门户重新生成密钥。Regenerate your key by using the Azure portal if you believe it may have been compromised.

维护存储连接字符串的最佳方法在配置文件中。The best way to maintain your storage connection string is in a configuration file. 若要配置连接字符串,请从 Visual Studio 中的解决方案资源管理器打开 app.config 文件。To configure your connection string, open the app.config file from Solution Explorer in Visual Studio. 添加 \<appSettings\> 元素的内容,如下所示。Add the contents of the \<appSettings\> element shown below. 将 connection-string 替换为你从门户中的存储帐户复制的值:Replace connection-string with the value you copied from your storage account in the portal:

<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
    <appSettings>
        <add key="StorageConnectionString" value="connection-string" />
    </appSettings>
</configuration>

例如,配置设置看起来类似于:For example, your configuration setting appears similar to:

<add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=storagesample;AccountKey=GMuzNHjlB3S9itqZJHHCnRkrokLkcSyW7yK9BRbGp0ENePunLPwBgpxV1Z/pVo9zpem/2xSHXkMqTHHLcx8XRA==;EndpointSuffix=core.chinacloudapi.cn" />

若要以 Azurite 存储仿真器为目标,可使用映射到已知帐户名称和密钥的快捷方式。To target the Azurite storage emulator, you can use a shortcut that maps to the well-known account name and key. 在这种情况下,连接字符串设置如下所示:In that case, your connection string setting is:

<add key="StorageConnectionString" value="UseDevelopmentStorage=true" />

添加 using 指令Add using directives

将以下 using 指令添加到 Program.cs 文件顶部:Add the following using directives to the top of the Program.cs file:

using System; // Namespace for Console output
using System.Configuration; // Namespace for ConfigurationManager
using System.Threading.Tasks; // Namespace for Task
using Azure.Storage.Queues; // Namespace for Queue storage types
using Azure.Storage.Queues.Models; // Namespace for PeekedMessage

创建队列服务客户端Create the Queue service client

使用 QueueClient 类可以检索存储在队列存储中的队列。The QueueClient class enables you to retrieve queues stored in Queue storage. 下面是创建服务客户端的一种方法:Here's one way to create the service client:

// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["storageConnectionString"];

// Instantiate a QueueClient which will be used to create and manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, "myqueue");

现在,已准备好编写从队列存储读取数据并将数据写入队列存储的代码。Now you are ready to write code that reads data from and writes data to Queue storage.

创建队列Create a queue

此示例演示如何创建队列:This example shows how to create a queue:

// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["storageConnectionString"];

// Instantiate a QueueClient which will be used to create and manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, "myqueue");

// Create the queue
queueClient.CreateIfNotExists();

在队列中插入消息Insert a message into a queue

若要在现有队列中插入消息,请调用 SendMessage 方法。To insert a message into an existing queue, call the SendMessage method. 消息可以是 string(UTF-8 格式)或 byte 数组。A message can be either a string (in UTF-8 format) or a byte array. 下面的代码将创建一个队列(如果该队列不存在)并插入一条消息:The following code creates a queue (if it doesn't exist) and inserts a message:

// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];

// Instantiate a QueueClient which will be used to create and manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, "myqueue");

if (queueClient.Exists())
{
    // Send a message to the queue
    queueClient.SendMessage(message);
}

扫视下一条消息Peek at the next message

可以通过调用 PeekMessages 方法来速览队列中的消息,而不必从队列中将其删除。You can peek at the messages in the queue without removing them from the queue by calling the PeekMessages method. 如果没有为 maxMessages 参数传递值,则默认设置是查看一条消息。If you don't pass a value for the maxMessages parameter, the default is to peek at one message.

// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];

// Instantiate a QueueClient which will be used to create and manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, "myqueue");

if (queueClient.Exists())
{ 
    // Peek at the next message
    PeekedMessage[] peekedMessage = queueClient.PeekMessages();

    // Display the message
    Console.WriteLine($"Peeked message: '{peekedMessage[0].MessageText}'");
}

更改已排队消息的内容Change the contents of a queued message

可以更改队列中现有消息的内容。You can change the contents of a message in-place in the queue. 如果消息表示工作任务,可使用此功能来更新该工作任务的状态。If the message represents a work task, you could use this feature to update the status of the work task. 以下代码使用新内容更新队列消息,并将可见性超时设置为再延长 60 秒。The following code updates the queue message with new contents, and sets the visibility timeout to extend another 60 seconds. 这会保存与消息关联的工作的状态,并额外为客户端提供一分钟的时间来继续处理消息。This saves the state of work associated with the message, and gives the client another minute to continue working on the message. 可使用此方法跟踪队列消息上的多步骤工作流,即使处理步骤因硬件或软件故障而失败,也无需从头开始操作。You could use this technique to track multi-step workflows on queue messages, without having to start over from the beginning if a processing step fails due to hardware or software failure. 通常同时保留重试计数,当消息重试次数超过 n 时再删除该消息。Typically, you would keep a retry count as well, and if the message is retried more than n times, you would delete it. 这可避免每次处理某条消息时都触发应用程序错误。This protects against a message that triggers an application error each time it is processed.

// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];

// Instantiate a QueueClient which will be used to create and manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, "myqueue");

if (queueClient.Exists())
{
    // Get the message from the queue
    QueueMessage[] message = queueClient.ReceiveMessages();

    // Update the message contents
    queueClient.UpdateMessage(message[0].MessageId, 
            message[0].PopReceipt, 
            "Updated contents",
            TimeSpan.FromSeconds(60.0)  // Make it invisible for another 60 seconds
        );
}

取消对下一条消息的排队De-queue the next message

可通过两个步骤取消消息在队列中的排队。De-queue a message from a queue in two steps. 调用 ReceiveMessages 时,可获得队列中的下一条消息。When you call ReceiveMessages, you get the next message in a queue. ReceiveMessages 返回的消息对于从此队列读取消息的任何其他代码都是不可见的。A message returned from ReceiveMessages becomes invisible to any other code reading messages from this queue. 默认情况下,此消息持续 30 秒不可见。By default, this message stays invisible for 30 seconds. 要从队列中删除消息,还必须调用 DeleteMessageTo finish removing the message from the queue, you must also call DeleteMessage. 此删除消息的两步过程可确保,如果代码因硬件或软件故障而无法处理消息,则代码的其他实例可以获取相同消息并重试。This two-step process of removing a message assures that if your code fails to process a message due to hardware or software failure, another instance of your code can get the same message and try again. 代码在处理消息后会立即调用 DeleteMessageYour code calls DeleteMessage right after the message has been processed.

// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];

// Instantiate a QueueClient which will be used to create and manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, "myqueue");

if (queueClient.Exists())
{
    // Get the next message
    QueueMessage[] retrievedMessage = queueClient.ReceiveMessages();

    // Process (i.e. print) the message in less than 30 seconds
    Console.WriteLine($"De-queued message: '{retrievedMessage[0].MessageText}'");

    // Delete the message
    queueClient.DeleteMessage(retrievedMessage[0].MessageId, retrievedMessage[0].PopReceipt);
}

将 Async-Await 模式与公用队列存储 API 配合使用Use Async-Await pattern with common Queue storage APIs

此示例演示如何将 Async-Await 模式和公用队列存储 API 配合使用。This example shows how to use the Async-Await pattern with common Queue storage APIs. 示例调用每个给定方法的异步版本,如每个方法的 Async 后缀所示。The sample calls the asynchronous version of each of the given methods, as indicated by the Async suffix of each method. 使用异步方法时,async-await 模式暂停本地执行,直到调用完成。When an async method is used, the async-await pattern suspends local execution until the call completes. 此行为允许当前的线程执行其他工作,这有助于避免性能瓶颈并提高应用程序的整体响应能力。This behavior allows the current thread to do other work, which helps avoid performance bottlenecks and improves the overall responsiveness of your application. 有关在 .NET 中使用 Async-Await 模式的详细信息,请参阅 Async 和 Await(C# 和 Visual Basic)For more details on using the Async-Await pattern in .NET see Async and Await (C# and Visual Basic)

// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];

// Instantiate a QueueClient which will be used to create and manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, "myqueue");

// Create the queue if it doesn't already exist
await queueClient.CreateIfNotExistsAsync();

if (await queueClient.ExistsAsync())
{
    Console.WriteLine($"Queue '{queueClient.Name}' created");
}
else
{
    Console.WriteLine($"Queue '{queueClient.Name}' exists");
}

// Async enqueue the message
await queueClient.SendMessageAsync("Hello, World");
Console.WriteLine($"Message added");

// Async receive the message
QueueMessage[] retrievedMessage = await queueClient.ReceiveMessagesAsync();
Console.WriteLine($"Retrieved message with content '{retrievedMessage[0].MessageText}'");

// Async delete the message
await queueClient.DeleteMessageAsync(retrievedMessage[0].MessageId, retrievedMessage[0].PopReceipt);
Console.WriteLine($"Deleted message: '{retrievedMessage[0].MessageText}'");

// Async delete the queue
await queueClient.DeleteAsync();
Console.WriteLine($"Deleted queue: '{queueClient.Name}'");

使用其他方法取消对消息的排队Leverage additional options for de-queuing messages

可通过两种方式自定义队列中消息的检索。There are two ways you can customize message retrieval from a queue. 首先,可获取一批消息(最多 32 条)。First, you can get a batch of messages (up to 32). 其次,可以设置更长或更短的不可见超时时间,从而允许代码使用更多或更少时间来完全处理每个消息。Second, you can set a longer or shorter invisibility timeout, allowing your code more or less time to fully process each message.

下面的代码示例使用 ReceiveMessages 方法,目的是在一次调用中获取 20 条消息。The following code example uses the ReceiveMessages method to get 20 messages in one call. 然后,使用 foreach 循环处理每条消息。Then it processes each message using a foreach loop. 它还将每条消息的不可见超时时间设置为 5 分钟。It also sets the invisibility timeout to five minutes for each message. 请注意,5 分钟超时时间对于所有消息都是同时开始的,因此在调用 ReceiveMessages 5 分钟后,尚未删除的任何消息都会再次变得可见。Note that the 5 minutes starts for all messages at the same time, so after 5 minutes have passed since the call to ReceiveMessages, any messages which have not been deleted will become visible again.

// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];

// Instantiate a QueueClient which will be used to create and manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, "myqueue");

if (queueClient.Exists())
{
    // Receive and process 20 messages
    QueueMessage[] receivedMessages = queueClient.ReceiveMessages(20, TimeSpan.FromMinutes(5));

    foreach (QueueMessage message in receivedMessages)
    {
        // Process (i.e. print) the messages in less than 5 minutes
        Console.WriteLine($"De-queued message: '{message.MessageText}'");

        // Delete the message
        queueClient.DeleteMessage(message.MessageId, message.PopReceipt);
    }
}

获取队列长度Get the queue length

可以获取队列中消息的估计数。You can get an estimate of the number of messages in a queue. GetProperties 方法要求队列服务检索队列属性,包括消息计数。The GetProperties method asks the Queue service to retrieve the queue properties, including the message count. ApproximateMessagesCount 属性包含队列中的大致消息数。The ApproximateMessagesCount property contains the approximate number of messages in the queue. 此数字不低于队列中的实际消息数,但可能会更高。This number is not lower than the actual number of messages in the queue, but could be higher.

// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];

// Instantiate a QueueClient which will be used to create and manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, "myqueue");

if (queueClient.Exists())
{
    QueueProperties properties = queueClient.GetProperties();

    // Retrieve the cached approximate message count.
    int cachedMessagesCount = properties.ApproximateMessagesCount;

    // Display number of messages.
    Console.WriteLine($"Number of messages in queue: {cachedMessagesCount}");
}

删除队列Delete a queue

若要删除队列及其包含的所有消息,请对队列对象调用 Delete 方法。To delete a queue and all the messages contained in it, call the Delete method on the queue object.

// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];

// Instantiate a QueueClient which will be used to create and manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, "myqueue");

if (queueClient.Exists())
{
    // Delete the queue
    queueClient.Delete();
}

后续步骤Next steps

现在,了解了有关队列存储的基础知识,可单击下面的链接来了解更复杂的存储任务。Now that you've learned the basics of Queue storage, follow these links to learn about more complex storage tasks.