Skip to content

Commit 6c076da

Browse files
committed
Add v12 support in tabs
1 parent 9c469c9 commit 6c076da

File tree

1 file changed

+101
-41
lines changed

1 file changed

+101
-41
lines changed

articles/storage/queues/storage-dotnet-how-to-use-queues.md

Lines changed: 101 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Azure Queues provide reliable, asynchronous messaging between appli
44
author: mhopkins-msft
55

66
ms.author: mhopkins
7-
ms.date: 05/21/2019
7+
ms.date: 05/08/2020
88
ms.service: storage
99
ms.subservice: queues
1010
ms.topic: conceptual
@@ -13,13 +13,13 @@ ms.reviewer: cbrooks
1313

1414
# Get started with Azure Queue storage using .NET
1515

16-
[!INCLUDE [storage-selector-queue-include](../../../includes/storage-selector-queue-include.md)]
16+
<!-- [!INCLUDE [storage-selector-queue-include](../../../includes/storage-selector-queue-include.md)] -->
1717

18-
[!INCLUDE [storage-check-out-samples-dotnet](../../../includes/storage-check-out-samples-dotnet.md)]
18+
<!-- [!INCLUDE [storage-check-out-samples-dotnet](../../../includes/storage-check-out-samples-dotnet.md)] -->
1919

2020
## Overview
2121

22-
Azure Queue storage provides cloud messaging between application components. In designing applications for scale, application components are often decoupled, so that they can scale independently. Queue storage delivers asynchronous messaging for communication 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.
22+
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.
2323

2424
### About this tutorial
2525

@@ -60,18 +60,38 @@ You can use the Azure Storage client libraries in any type of .NET application,
6060

6161
### Use NuGet to install the required packages
6262

63+
# [\.NET v12](#tab/dotnet)
64+
65+
You need to reference the following four packages in your project to complete this tutorial:
66+
67+
- [Azure Storage Common Client Library for .NET](https://www.nuget.org/packages/Azure.Storage.Common/): This package provides programmatic access to data resources in your storage account.
68+
- [Azure Storage Queue Library for .NET](https://www.nuget.org/packages/Azure.Storage.Queues/): This client library enables working with the Microsoft Azure Storage Queue service for storing messages that may be accessed by a client.
69+
- [Azure Core library for .NET](https://www.nuget.org/packages/Azure.Core/): This package provides a class for parsing a connection string in a configuration file, regardless of where your application is running.
70+
- [Configuration Manager library for .NET](https://www.nuget.org/packages/System.Configuration.ConfigurationManager/): This package provides a class for retrieving the connection string from a configuration file.
71+
72+
You can use NuGet to obtain these packages. Follow these steps:
73+
74+
1. Right-click your project in **Solution Explorer**, and choose **Manage NuGet Packages**.
75+
1. Select **Browse**
76+
1. Search online for "Azure.Storage.Queues", and select **Install** to install the Storage client library and its dependencies. This will also install the Azure.Storage.Common and Azure.Core libraries, which are dependencies of the queue library.
77+
1. Search online for "System.Configuration.ConfigurationManager", and select **Install** to install the Configuration Manager.
78+
79+
# [\.NET v11](#tab/dotnetv11)
80+
6381
You need to reference the following three packages in your project to complete this tutorial:
6482

65-
* [Microsoft Azure Storage Common Client Library for .NET](https://www.nuget.org/packages/Microsoft.Azure.Storage.Common/): This package provides programmatic access to data resources in your storage account.
66-
* [Microsoft Azure Storage Queue Library for .NET](https://www.nuget.org/packages/Microsoft.Azure.Storage.Queue/): This client library enables working with the Microsoft Azure Storage Queue service for storing messages that may be accessed by a client.
67-
* [Microsoft Azure Configuration Manager library for .NET](https://www.nuget.org/packages/Microsoft.Azure.ConfigurationManager/): This package provides a class for parsing a connection string in a configuration file, regardless of where your application is running.
83+
- [Microsoft Azure Storage Common Client Library for .NET](https://www.nuget.org/packages/Microsoft.Azure.Storage.Common/): This package provides programmatic access to data resources in your storage account.
84+
- [Microsoft Azure Storage Queue Library for .NET](https://www.nuget.org/packages/Microsoft.Azure.Storage.Queue/): This client library enables working with the Microsoft Azure Storage Queue service for storing messages that may be accessed by a client.
85+
- [Microsoft Azure Configuration Manager library for .NET](https://www.nuget.org/packages/Microsoft.Azure.ConfigurationManager/): This package provides a class for parsing a connection string in a configuration file, regardless of where your application is running.
6886

6987
You can use NuGet to obtain these packages. Follow these steps:
7088

7189
1. Right-click your project in **Solution Explorer**, and choose **Manage NuGet Packages**.
72-
2. Select **Browse**
73-
3. Search online for "Microsoft.Azure.Storage.Queue", and select **Install** to install the Storage client library and its dependencies. This will also install the Microsoft.Azure.Storage.Common library, which is a dependency of the queue library.
74-
4. Search online for "Microsoft.Azure.ConfigurationManager", and select **Install** to install the Azure Configuration Manager.
90+
1. Select **Browse**
91+
1. Search online for "Microsoft.Azure.Storage.Queue", and select **Install** to install the Storage client library and its dependencies. This will also install the Microsoft.Azure.Storage.Common library, which is a dependency of the queue library.
92+
1. Search online for "Microsoft.Azure.ConfigurationManager", and select **Install** to install the Azure Configuration Manager.
93+
94+
---
7595

7696
> [!NOTE]
7797
> The Storage client libraries packages are also included in the [Azure SDK for .NET](https://azure.microsoft.com/downloads/). However, we recommend that you also install the Storage client libraries from NuGet to ensure that you always have the latest versions.
@@ -82,102 +102,142 @@ You can use NuGet to obtain these packages. Follow these steps:
82102

83103
You have two environment options for running the examples in this guide:
84104

85-
* You can run your code against an Azure Storage account in the cloud.
86-
* You can run your code against the Azure storage emulator. The storage emulator is a local environment that emulates an Azure Storage account in the cloud. The emulator 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. For more information, see [Use the Azure storage emulator for development and testing](../common/storage-use-emulator.md).
87-
88-
If you are targeting a storage account in the cloud, copy the primary access key for your storage account from the Azure portal. For more information, see [Manage storage account access keys](../common/storage-account-keys-manage.md).
105+
- You can run your code against an Azure Storage account in the cloud.
106+
- You can run your code against the Azurite storage emulator. Azurite is a local environment that emulates an Azure Storage account in the cloud. 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. For more information, see [Use the Azurite emulator for local Azure Storage development and testing](../common/storage-use-azurite.md).
89107

90108
> [!NOTE]
91109
> You can target the storage emulator to avoid incurring any costs associated with Azure Storage. However, if you do choose to target an Azure storage account in the cloud, costs for performing this tutorial will be negligible.
92110
93-
### Configure your storage connection string
111+
## Configure your storage connection string
112+
113+
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](../common/storage-account-keys-manage.md).
114+
115+
### Copy your credentials from the Azure portal
116+
117+
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:
118+
119+
1. Navigate to the [Azure portal](https://portal.azure.com).
120+
2. Locate your storage account.
121+
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.
122+
4. 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.
94123

95-
The Azure Storage client libraries for .NET support using a storage connection string to configure endpoints and credentials for accessing storage services. The best way to maintain your storage connection string is in a configuration file.
124+
![Screenshot showing how to copy a connection string from the Azure portal](media/storage-dotnet-how-to-use-queues/portal-connection-string.png)
96125

97126
For more information about connection strings, see [Configure a connection string to Azure Storage](../common/storage-configure-connection-string.md).
98127

99128
> [!NOTE]
100129
> 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. Regenerate your key by using the Azure portal if you believe it may have been compromised.
101130
102-
To configure your connection string, open the **app.config** file from Solution Explorer in Visual Studio. Add the contents of the **\<appSettings\>** element shown below. Replace *account-name* with the name of your storage account, and *account-key* with your account access key:
131+
The best way to maintain your storage connection string is in a configuration file. To configure your connection string, open the **app.config** file from Solution Explorer in Visual Studio. Add the contents of the **\<appSettings\>** element shown below. Replace *connection-string* with the value you copied from your storage account in the portal:
103132

104133
```xml
105134
<configuration>
106135
<startup>
107-
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
136+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
108137
</startup>
109138
<appSettings>
110-
<add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key" />
139+
<add key="StorageConnectionString" value="connection-string" />
111140
</appSettings>
112141
</configuration>
113142
```
114143

115144
For example, your configuration setting appears similar to:
116145

117146
```xml
118-
<add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=storagesample;AccountKey=GMuzNHjlB3S9itqZJHHCnRkrokLkcSyW7yK9BRbGp0ENePunLPwBgpxV1Z/pVo9zpem/2xSHXkMqTHHLcx8XRA==" />
147+
<add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=storagesample;AccountKey=GMuzNHjlB3S9itqZJHHCnRkrokLkcSyW7yK9BRbGp0ENePunLPwBgpxV1Z/pVo9zpem/2xSHXkMqTHHLcx8XRA==EndpointSuffix=core.windows.net" />
119148
```
120149

121-
To target the 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:
150+
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:
122151

123152
```xml
124-
<add key="StorageConnectionString" value="UseDevelopmentStorage=true;" />
153+
<add key="StorageConnectionString" value="DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;" />
125154
```
126155

127156
### Add using directives
128157

129158
Add the following `using` directives to the top of the `Program.cs` file:
130159

160+
# [\.NET v12](#tab/dotnet)
161+
131162
```csharp
163+
using System;
164+
using System.Configuration; // Namespace for ConfigurationManager
165+
using Azure.Storage.Queues; // Namespace for Queue storage types
166+
using Azure.Storage.Queues.Models; // Namespace for Queue models
167+
```
168+
169+
# [\.NET v11](#tab/dotnetv11)
170+
171+
```csharp
172+
using System;
132173
using Microsoft.Azure; // Namespace for CloudConfigurationManager
133174
using Microsoft.Azure.Storage; // Namespace for CloudStorageAccount
134175
using Microsoft.Azure.Storage.Queue; // Namespace for Queue storage types
135176
```
136177

137-
### Copy your credentials from the Azure portal
178+
---
138179

139-
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:
180+
### Create the Queue service client
140181

141-
1. Navigate to the [Azure portal](https://portal.azure.com).
142-
2. Locate your storage account.
143-
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.
144-
4. 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.
182+
# [\.NET v12](#tab/dotnet)
145183

146-
![Screenshot showing how to copy a connection string from the Azure portal](media/storage-dotnet-how-to-use-queues/portal-connection-string.png)
184+
The [QueueClient](/dotnet/api/azure.storage.queues.queueclient) class enables you to retrieve queues stored in Queue storage. Here's one way to create the service client:
147185

148-
### Parse the connection string
186+
```csharp
187+
// Get the connection string from app settings
188+
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
149189

150-
[!INCLUDE [storage-cloud-configuration-manager-include](../../../includes/storage-cloud-configuration-manager-include.md)]
190+
// Create a unique name for the queue
191+
string queueName = "queues-" + Guid.NewGuid().ToString();
151192

152-
### Create the Queue service client
193+
// Instantiate a QueueClient which will be
194+
// used to create and manipulate the queue
195+
QueueClient queueClient = new QueueClient(connectionString, queueName);
196+
```
197+
198+
# [\.NET v11](#tab/dotnetv11)
153199

154-
The [CloudQueueClient](https://docs.microsoft.com/dotnet/api/microsoft.azure.storage.queue.cloudqueueclient?view=azure-dotnet) class enables you to retrieve queues stored in Queue storage. Here's one way to create the service client:
200+
The [CloudQueueClient](/dotnet/api/microsoft.azure.storage.queue.cloudqueueclient?view=azure-dotnet-legacy) class enables you to retrieve queues stored in Queue storage. Here's one way to create the service client:
155201

156202
```csharp
203+
// Retrieve storage account from connection string
204+
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
205+
CloudConfigurationManager.GetSetting("StorageConnectionString"));
206+
207+
// Create the queue client
157208
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
158209
```
159210

211+
---
212+
160213
Now you are ready to write code that reads data from and writes data to Queue storage.
161214

162215
## Create a queue
163216

164-
This example shows how to create a queue if it does not already exist:
217+
This example shows how to create a queue:
218+
219+
# [\.NET v12](#tab/dotnet)
165220

166221
```csharp
167-
// Retrieve storage account from connection string.
168-
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
169-
CloudConfigurationManager.GetSetting("StorageConnectionString"));
222+
// Create the queue
223+
queueClient.CreateIfNotExists();
224+
```
170225

171-
// Create the queue client.
172-
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
226+
# [\.NET v11](#tab/dotnetv11)
173227

174-
// Retrieve a reference to a container.
175-
CloudQueue queue = queueClient.GetQueueReference("myqueue");
228+
```csharp
229+
// Create a unique name for the queue
230+
string queueName = "queues-" + Guid.NewGuid().ToString();
231+
232+
// Retrieve a reference to a container
233+
CloudQueue queue = queueClient.GetQueueReference(queueName);
176234

177235
// Create the queue if it doesn't already exist
178236
queue.CreateIfNotExists();
179237
```
180238

239+
---
240+
181241
## Insert a message into a queue
182242

183243
To insert a message into an existing queue, first create a new [CloudQueueMessage](https://docs.microsoft.com/dotnet/api/microsoft.azure.storage.queue.cloudqueuemessage?view=azure-dotnet). Next, call the [AddMessage](https://docs.microsoft.com/dotnet/api/microsoft.azure.storage.queue.cloudqueue.addmessage?view=azure-dotnet) method. A

0 commit comments

Comments
 (0)