Skip to content

Commit cb279f4

Browse files
author
Jill Grant
authored
Merge pull request #283913 from normesta/normesta-reg-updates-14
Removing connection strings
2 parents 42c117c + 2d2251b commit cb279f4

File tree

1 file changed

+112
-25
lines changed

1 file changed

+112
-25
lines changed

articles/storage/queues/storage-tutorial-queues.md

Lines changed: 112 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: A tutorial on using the Azure Queue Storage to create queues, and i
44
author: normesta
55
ms.author: normesta
66
ms.reviewer: dineshm
7-
ms.date: 06/09/2020
7+
ms.date: 08/07/2024
88
ms.topic: tutorial
99
ms.service: azure-queue-storage
1010
ms.devlang: csharp
@@ -39,7 +39,11 @@ In this tutorial, you learn how to:
3939

4040
## Create an Azure Storage account
4141

42-
First, create an Azure Storage account. For a step-by-step guide to creating a storage account, see [Create a storage account](../common/storage-account-create.md?toc=/azure/storage/queues/toc.json). This is a separate step you perform after creating a free Azure account in the prerequisites.
42+
1. First, create an Azure Storage account.
43+
44+
For a step-by-step guide to creating a storage account, see [Create a storage account](../common/storage-account-create.md?toc=/azure/storage/queues/toc.json). This is a separate step you perform after creating a free Azure account in the prerequisites.
45+
46+
2. Make sure that your user account has been assigned the [Storage Queue Data Contributor](storage-quickstart-queues-dotnet.md#authenticate-to-azure) role, scoped to the storage account, parent resource group, or subscription. See [Authenticate to Azure](storage-quickstart-queues-dotnet.md#authenticate-to-azure).
4347

4448
## Create the app
4549

@@ -127,25 +131,7 @@ Since the app uses cloud resources, the code runs asynchronously.
127131

128132
## Create a queue
129133

130-
Before making any calls into Azure APIs, you must get your credentials from the Azure portal.
131-
132-
[!INCLUDE [storage-quickstart-credentials-include](../../../includes/storage-quickstart-credentials-include.md)]
133-
134-
### Add the connection string to the app
135-
136-
Add the connection string into the app so it can access the storage account.
137-
138-
1. Switch back to Visual Studio Code.
139-
140-
1. In the `Main` method, replace the `Console.WriteLine("Hello, World");` code with the following line that gets the connection string from the environment variable.
141-
142-
:::code language="csharp" source="~/azure-storage-snippets/queues/tutorial/dotnet/dotnet-v12/QueueApp/Program.cs" id="snippet_DeclareConnectionString":::
143-
144-
1. Add the following code to `Main` to create a queue object, which is later passed into the send and receive methods.
145-
146-
:::code language="csharp" source="~/azure-storage-snippets/queues/tutorial/dotnet/dotnet-v12/QueueApp/Program.cs" id="snippet_CreateQueueClient":::
147-
148-
1. Save the file.
134+
Before making any calls into Azure APIs, you must make sure you're authenticated with the same Microsoft Entra account you assigned the role to. Once authenticated, you can create and authorize a `QueueClient` object by using `DefaultAzureCredential` to access queue data in the storage account. `DefaultAzureCredential` automatically discovers and uses the account you signed into. To learn how to sign in and then create a `QueueClient` object, see [Authorize access and create a client object](storage-quickstart-queues-dotnet.md#authorize-access-and-create-a-client-object).
149135

150136
## Insert messages into the queue
151137

@@ -199,17 +185,118 @@ If there are no command-line arguments, attempt a retrieve operation. Call the `
199185

200186
Finally, wait for user input before exiting by calling `Console.ReadLine`.
201187

202-
1. Expand the `Main` method to check for command-line arguments and wait for user input.
188+
1. Expand the `Main` method to check for command-line arguments and wait for user input. In the code snippet below, make sure to replace the `{storageAccountName}` placeholder with the name of your storage account.
203189

204-
:::code language="csharp" source="~/azure-storage-snippets/queues/tutorial/dotnet/dotnet-v12/QueueApp/Program.cs" id="snippet_Main":::
190+
```csharp
191+
static async Task Main(string[] args)
192+
{
193+
QueueClient queue = new QueueClient(
194+
new Uri($"https://{storageAccountName}.queue.core.windows.net/mystoragequeue"),
195+
new DefaultAzureCredential());
196+
197+
if (args.Length > 0)
198+
{
199+
string value = String.Join(" ", args);
200+
await InsertMessageAsync(queue, value);
201+
Console.WriteLine($"Sent: {value}");
202+
}
203+
else
204+
{
205+
string value = await RetrieveNextMessageAsync(queue);
206+
Console.WriteLine($"Received: {value}");
207+
}
208+
209+
Console.Write("Press Enter...");
210+
Console.ReadLine();
211+
}
212+
```
205213

206-
1. Save the file.
214+
2. Save the file.
207215

208216
## Complete code
209217

210218
Here is the complete code listing for this project.
211219

212-
:::code language="csharp" source="~/azure-storage-snippets/queues/tutorial/dotnet/dotnet-v12/QueueApp/Program.cs" id="snippet_AllCode":::
220+
```csharp
221+
using System;
222+
using System.Threading.Tasks;
223+
using Azure.Storage.Queues;
224+
using Azure.Storage.Queues.Models;
225+
using Azure.Identity;
226+
227+
namespace QueueApp
228+
{
229+
class Program
230+
{
231+
static async Task Main(string[] args)
232+
{
233+
QueueClient queue = new QueueClient(
234+
new Uri($"https://{storageAccountName}.queue.core.windows.net/mystoragequeue"),
235+
new DefaultAzureCredential());
236+
237+
if (args.Length > 0)
238+
{
239+
string value = String.Join(" ", args);
240+
await InsertMessageAsync(queue, value);
241+
Console.WriteLine($"Sent: {value}");
242+
}
243+
else
244+
{
245+
string value = await RetrieveNextMessageAsync(queue);
246+
Console.WriteLine($"Received: {value}");
247+
}
248+
249+
Console.Write("Press Enter...");
250+
Console.ReadLine();
251+
}
252+
253+
static async Task InsertMessageAsync(QueueClient theQueue, string newMessage)
254+
{
255+
if (null != await theQueue.CreateIfNotExistsAsync())
256+
{
257+
Console.WriteLine("The queue was created.");
258+
}
259+
260+
await theQueue.SendMessageAsync(newMessage);
261+
}
262+
263+
static async Task<string> RetrieveNextMessageAsync(QueueClient theQueue)
264+
{
265+
if (await theQueue.ExistsAsync())
266+
{
267+
QueueProperties properties = await theQueue.GetPropertiesAsync();
268+
269+
if (properties.ApproximateMessagesCount > 0)
270+
{
271+
QueueMessage[] retrievedMessage = await theQueue.ReceiveMessagesAsync(1);
272+
string theMessage = retrievedMessage[0].Body.ToString();
273+
await theQueue.DeleteMessageAsync(retrievedMessage[0].MessageId, retrievedMessage[0].PopReceipt);
274+
return theMessage;
275+
}
276+
else
277+
{
278+
Console.Write("The queue is empty. Attempt to delete it? (Y/N) ");
279+
string response = Console.ReadLine();
280+
281+
if (response.ToUpper() == "Y")
282+
{
283+
await theQueue.DeleteIfExistsAsync();
284+
return "The queue was deleted.";
285+
}
286+
else
287+
{
288+
return "The queue was not deleted.";
289+
}
290+
}
291+
}
292+
else
293+
{
294+
return "The queue does not exist. Add a message to the command line to create the queue and store the message.";
295+
}
296+
}
297+
}
298+
}
299+
```
213300

214301
## Build and run the app
215302

0 commit comments

Comments
 (0)