You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: Learn how to use the Azure Queue Storage client library v12 for .NET to create a queue and add messages to the queue. Next, you learn how to read and delete messages from the queue. You'll also learn how to delete a queue.
description: Learn how to use the Azure Queue Storage client library for .NET to create a queue and add messages to the queue. Next, you learn how to read and delete messages from the queue. You'll also learn how to delete a queue.
# Quickstart: Azure Queue Storage client library v12 for .NET
14
+
# Quickstart: Azure Queue Storage client library for .NET
15
15
16
16
Get started with the Azure Queue Storage client library version 12 for .NET. Azure Queue Storage is a service for storing large numbers of messages for later retrieval and processing. Follow these steps to install the package and try out example code for basic tasks.
17
17
18
-
Use the Azure Queue Storage client library v12 for .NET to:
18
+
Use the Azure Queue Storage client library for .NET to:
19
19
20
20
- Create a queue
21
21
- Add messages to a queue
@@ -40,22 +40,22 @@ Additional resources:
40
40
41
41
## Setting up
42
42
43
-
This section walks you through preparing a project to work with the Azure Queue Storage client library v12 for .NET.
43
+
This section walks you through preparing a project to work with the Azure Queue Storage client library for .NET.
44
44
45
45
### Create the project
46
46
47
-
Create a .NET Core application named `QueuesQuickstartV12`.
47
+
Create a .NET Core application named `QueuesQuickstart`.
48
48
49
-
1. In a console window (such as cmd, PowerShell, or Bash), use the `dotnet new` command to create a new console app with the name `QueuesQuickstartV12`. This command creates a simple "hello world" C# project with a single source file named `Program.cs`.
49
+
1. In a console window (such as cmd, PowerShell, or Bash), use the `dotnet new` command to create a new console app with the name `QueuesQuickstart`. This command creates a simple "hello world" C# project with a single source file named `Program.cs`.
50
50
51
51
```console
52
-
dotnet new console -n QueuesQuickstartV12
52
+
dotnet new console -n QueuesQuickstart
53
53
```
54
54
55
-
1. Switch to the newly created `QueuesQuickstartV12` directory.
55
+
1. Switch to the newly created `QueuesQuickstart` directory.
Azure Queue Storage is a service for storing large numbers of messages. A queue message can be up to 64 KB in size. A queue may contain millions of messages, up to the total capacity limit of a storage account. Queues are commonly used to create a backlog of work to process asynchronously. Queue Storage offers three types of resources:
@@ -119,23 +121,71 @@ Use the following .NET classes to interact with these resources:
119
121
120
122
These example code snippets show you how to perform the following actions with the Azure Queue Storage client library for .NET:
121
123
122
-
-[Get the connection string](#get-the-connection-string)
123
-
-[Create a queue](#create-a-queue)
124
+
-[Authenticate and create the client](#add-the-azure-identity-client-library)
125
+
-[Create a queue](#create-a-queue-using-the-queueclient)
124
126
-[Add messages to a queue](#add-messages-to-a-queue)
125
127
-[Peek at messages in a queue](#peek-at-messages-in-a-queue)
126
128
-[Update a message in a queue](#update-a-message-in-a-queue)
127
129
-[Receive messages from a queue](#receive-messages-from-a-queue)
128
130
-[Delete messages from a queue](#delete-messages-from-a-queue)
You can authenticate a `QueueClient` to Storage Queue using `DefaultAzureCredential` by adding the `Azure.Identity` NuGet package to your application. `DefaultAzureCredential` will automatically discover and use the account you signed-in with in the previous step.
140
+
141
+
```dotnetcli
142
+
dotnet add package Azure.Identity
143
+
```
144
+
145
+
At the top of the `Program.cs` file, add a using directive for the `Azure.Identity` namespace.
The following code retrieves the connection string for the storage account. The connection string is stored in the environment variable created in the [Configure your storage connection string](#configure-your-storage-connection-string) section.
134
184
135
-
Add this code inside the `Main` method:
185
+
Add this code to the end of the `Program.cs` file:
Decide on a name for the new queue. The following code appends a GUID value to the queue name to ensure that it's unique.
152
202
@@ -155,7 +205,7 @@ Decide on a name for the new queue. The following code appends a GUID value to t
155
205
156
206
Create an instance of the [`QueueClient`](/dotnet/api/azure.storage.queues.queueclient) class. Then, call the [`CreateAsync`](/dotnet/api/azure.storage.queues.queueclient.createasync) method to create the queue in your storage account.
157
207
158
-
Add this code to the end of the `Main` method:
208
+
Add this code to the end of the `Program.cs` file:
159
209
160
210
```csharp
161
211
// Create a unique name for the queue
@@ -171,11 +221,16 @@ QueueClient queueClient = new QueueClient(connectionString, queueName);
171
221
awaitqueueClient.CreateAsync();
172
222
```
173
223
224
+
> [!IMPORTANT]
225
+
> The account access key should be used with caution. If your account access key is lost or accidentally placed in an insecure location, your service may become vulnerable. Anyone who has the access key is able to authorize requests against the storage account, and effectively has access to all the data. `DefaultAzureCredential` provides enhanced security features and benefits and is the recommended approach for managing authorization to Azure services
226
+
227
+
---
228
+
174
229
### Add messages to a queue
175
230
176
231
The following code snippet asynchronously adds messages to queue by calling the [`SendMessageAsync`](/dotnet/api/azure.storage.queues.queueclient.sendmessageasync) method. It also saves a [`SendReceipt`](/dotnet/api/azure.storage.queues.models.sendreceipt) returned from a `SendMessageAsync` call. The receipt is used to update the message later in the program.
177
232
178
-
Add this code to the end of the `Main` method:
233
+
Add this code to the end of the `Program.cs` file:
179
234
180
235
```csharp
181
236
Console.WriteLine("\nAdding messages to the queue...");
Peek at the messages in the queue by calling the [`PeekMessagesAsync`](/dotnet/api/azure.storage.queues.queueclient.peekmessagesasync) method. This method retrieves one or more messages from the front of the queue but doesn't alter the visibility of the message.
194
249
195
-
Add this code to the end of the `Main` method:
250
+
Add this code to the end of the `Program.cs` file:
196
251
197
252
```csharp
198
253
Console.WriteLine("\nPeek at the messages in the queue...");
Delete messages from the queue after they're been processed. In this case, processing is just displaying the message on the console.
291
+
Delete messages from the queue after they've been processed. In this case, processing is just displaying the message on the console.
237
292
238
293
The app pauses for user input by calling `Console.ReadLine` before it processes and deletes the messages. Verify in your [Azure portal](https://portal.azure.com) that the resources were created correctly, before they're deleted. Any messages not explicitly deleted will eventually become visible in the queue again for another chance to process them.
239
294
240
-
Add this code to the end of the `Main` method:
295
+
Add this code to the end of the `Program.cs` file:
241
296
242
297
```csharp
243
298
Console.WriteLine("\nPress Enter key to 'process' messages and delete them from the queue...");
@@ -259,7 +314,7 @@ foreach (QueueMessage message in messages)
259
314
260
315
The following code cleans up the resources the app created by deleting the queue using the [`DeleteAsync`](/dotnet/api/azure.storage.queues.queueclient.deleteasync) method.
261
316
262
-
Add this code to the end of the `Main` method:
317
+
Add this code to the end of the `Program.cs` file:
263
318
264
319
```csharp
265
320
Console.WriteLine("\nPress Enter key to delete the queue...");
@@ -289,7 +344,7 @@ dotnet run
289
344
The output of the app is similar to the following example:
When developing locally, make sure that the user account that is accessing the queue data has the correct permissions. You'll need **Storage Queue Data Contributor** to read and write queue data. To assign yourself this role, you'll need to be assigned the **User Access Administrator** role, or another role that includes the **Microsoft.Authorization/roleAssignments/write** action. You can assign Azure RBAC roles to a user using the Azure portal, Azure CLI, or Azure PowerShell. You can learn more about the available scopes for role assignments on the [scope overview](../../../articles/role-based-access-control/scope-overview.md) page.
14
+
15
+
In this scenario, you'll assign permissions to your user account, scoped to the storage account, to follow the [Principle of Least Privilege](../../../articles/active-directory/develop/secure-least-privileged-access.md). This practice gives users only the minimum permissions needed and creates more secure production environments.
16
+
17
+
The following example will assign the **Storage Queue Data Contributor** role to your user account, which provides both read and write access to queue data in your storage account.
18
+
19
+
> [!IMPORTANT]
20
+
> In most cases it will take a minute or two for the role assignment to propagate in Azure, but in rare cases it may take up to eight minutes. If you receive authentication errors when you first run your code, wait a few moments and try again.
21
+
22
+
### [Azure portal](#tab/roles-azure-portal)
23
+
24
+
1. In the Azure portal, locate your storage account using the main search bar or left navigation.
25
+
26
+
2. On the storage account overview page, select **Access control (IAM)** from the left-hand menu.
27
+
28
+
3. On the **Access control (IAM)** page, select the **Role assignments** tab.
29
+
30
+
4. Select **+ Add** from the top menu and then **Add role assignment** from the resulting drop-down menu.
31
+
32
+
:::image type="content" source="../../../articles/storage/common/media/assign-role-system-identity.png" alt-text="A screenshot showing how to assign a role.":::
33
+
34
+
5. Use the search box to filter the results to the desired role. For this example, search for *Storage Queue Data Contributor* and select the matching result and then choose **Next**.
35
+
36
+
6. Under **Assign access to**, select **User, group, or service principal**, and then choose **+ Select members**.
37
+
38
+
7. In the dialog, search for your Azure AD username (usually your *user@domain* email address) and then choose **Select** at the bottom of the dialog.
39
+
40
+
8. Select **Review + assign** to go to the final page, and then **Review + assign** again to complete the process.
41
+
42
+
### [Azure CLI](#tab/roles-azure-cli)
43
+
44
+
To assign a role at the resource level using the Azure CLI, you first must retrieve the resource id using the `az storage account show` command. You can filter the output properties using the `--query` parameter.
45
+
46
+
```azurecli
47
+
az storage account show --resource-group '<your-resource-group-name>' --name '<your-storage-account-name>' --query id
48
+
```
49
+
50
+
Copy the output `Id` from the preceding command. You can then assign roles using the [az role](/cli/azure/role) command of the Azure CLI.
51
+
52
+
```azurecli
53
+
az role assignment create --assignee "<user@domain>" \
54
+
--role "Storage Queue Data Contributor" \
55
+
--scope "<your-resource-id>"
56
+
```
57
+
58
+
### [PowerShell](#tab/roles-powershell)
59
+
60
+
To assign a role at the resource level using Azure PowerShell, you first must retrieve the resource ID using the `Get-AzResource` command.
Copy the `Id` value from the preceding command output. You can then assign roles using the [New-AzRoleAssignment](/powershell/module/az.resources/new-azroleassignment) command in PowerShell.
67
+
68
+
```azurepowershell
69
+
New-AzRoleAssignment -SignInName <user@domain> `
70
+
-RoleDefinitionName "Storage Queue Data Contributor" `
0 commit comments