Skip to content

Commit ac60fb6

Browse files
committed
Snippetized the v12 code
1 parent e2aa947 commit ac60fb6

File tree

1 file changed

+12
-175
lines changed

1 file changed

+12
-175
lines changed

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

Lines changed: 12 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,7 @@ Add the following `using` directives to the top of the `Program.cs` file:
155155

156156
# [\.NET v12](#tab/dotnet)
157157

158-
```csharp
159-
using System; // Namespace for Console output
160-
using System.Configuration; // Namespace for ConfigurationManager
161-
using Azure.Storage.Queues; // Namespace for Queue storage types
162-
using Azure.Storage.Queues.Models; // Namespace for PeekedMessage
163-
```
158+
:::code language="csharp" source="~/azure-storage-snippets/queues/howto/dotnet/dotnet-v12/QueueBasics.cs" id="snippet_UsingStatements":::
164159

165160
# [\.NET v11](#tab/dotnetv11)
166161

@@ -179,13 +174,7 @@ using Microsoft.Azure.Storage.Queue; // Namespace for Queue storage types
179174

180175
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:
181176

182-
```csharp
183-
// Get the connection string from app settings
184-
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
185-
186-
// Instantiate a QueueClient which will be used to create and manipulate the queue
187-
QueueClient queueClient = new QueueClient(connectionString, "myqueue");
188-
```
177+
:::code language="csharp" source="~/azure-storage-snippets/queues/howto/dotnet/dotnet-v12/QueueBasics.cs" id="snippet_CreateClient":::
189178

190179
# [\.NET v11](#tab/dotnetv11)
191180

@@ -210,16 +199,7 @@ This example shows how to create a queue:
210199

211200
# [\.NET v12](#tab/dotnet)
212201

213-
```csharp
214-
// Get the connection string from app settings
215-
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
216-
217-
// Instantiate a QueueClient which will be used to create and manipulate the queue
218-
QueueClient queueClient = new QueueClient(connectionString, "myqueue");
219-
220-
// Create the queue
221-
queueClient.CreateIfNotExists();
222-
```
202+
:::code language="csharp" source="~/azure-storage-snippets/queues/howto/dotnet/dotnet-v12/QueueBasics.cs" id="snippet_CreateQueue":::
223203

224204
# [\.NET v11](#tab/dotnetv11)
225205

@@ -246,19 +226,7 @@ queue.CreateIfNotExists();
246226

247227
To insert a message into an existing queue, call the [SendMessage](/dotnet/api/azure.storage.queues.queueclient.sendmessage) method. A can be either a `string` (in UTF-8 format) or a `byte` array. Here is code which creates a queue (if it doesn't exist) and inserts the message "Hello, World":
248228

249-
```csharp
250-
// Get the connection string from app settings
251-
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
252-
253-
// Instantiate a QueueClient which will be used to create and manipulate the queue
254-
QueueClient queueClient = new QueueClient(connectionString, "myqueue");
255-
256-
if (queueClient.Exists())
257-
{
258-
// Send a message to the queue
259-
queueClient.SendMessage("Hello, World");
260-
}
261-
```
229+
:::code language="csharp" source="~/azure-storage-snippets/queues/howto/dotnet/dotnet-v12/QueueBasics.cs" id="snippet_InsertMessage":::
262230

263231
# [\.NET v11](#tab/dotnetv11)
264232

@@ -291,22 +259,7 @@ queue.AddMessage(message);
291259

292260
You can peek at the messages in the queue without removing them from the queue by calling the [PeekMessages](/dotnet/api/azure.storage.queues.queueclient.peekmessages) method. If you don't pass a value for the *maxResults* parameter, the default is to peek at one message.
293261

294-
```csharp
295-
// Get the connection string from app settings
296-
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
297-
298-
// Instantiate a QueueClient which will be used to create and manipulate the queue
299-
QueueClient queueClient = new QueueClient(connectionString, "myqueue");
300-
301-
if (queueClient.Exists())
302-
{
303-
// Peek at the next message
304-
PeekedMessage[] peekedMessage = queueClient.PeekMessages();
305-
306-
// Display the message
307-
Console.WriteLine(peekedMessage[0].MessageText);
308-
}
309-
```
262+
:::code language="csharp" source="~/azure-storage-snippets/queues/howto/dotnet/dotnet-v12/QueueBasics.cs" id="snippet_PeekMessage":::
310263

311264
# [\.NET v11](#tab/dotnetv11)
312265

@@ -338,26 +291,7 @@ You can change the contents of a message in-place in the queue. If the message r
338291

339292
# [\.NET v12](#tab/dotnet)
340293

341-
```csharp
342-
// Get the connection string from app settings
343-
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
344-
345-
// Instantiate a QueueClient which will be used to create and manipulate the queue
346-
QueueClient queueClient = new QueueClient(connectionString, "myqueue");
347-
348-
if (queueClient.Exists())
349-
{
350-
// Get the message from the queue
351-
QueueMessage[] message = queueClient.ReceiveMessages();
352-
353-
// Update the message contents
354-
queueClient.UpdateMessage(message[0].MessageId,
355-
message[0].PopReceipt,
356-
"Updated contents.",
357-
TimeSpan.FromSeconds(60.0) // Make it invisible for another 60 seconds
358-
);
359-
}
360-
```
294+
:::code language="csharp" source="~/azure-storage-snippets/queues/howto/dotnet/dotnet-v12/QueueBasics.cs" id="snippet_UpdateMessage":::
361295

362296
# [\.NET v11](#tab/dotnetv11)
363297

@@ -386,26 +320,9 @@ queue.UpdateMessage(message,
386320

387321
# [\.NET v12](#tab/dotnet)
388322

389-
Your code de-queues a message from a queue in two steps. When you call [ReceiveMessages](/dotnet/api/azure.storage.queues.queueclient.receivemessages), you get the next message in a queue. A message returned from `ReceiveMessages` becomes invisible to any other code reading messages from this queue. By default, this message stays invisible for 30 seconds. To finish removing the message from the queue, you must also call [DeleteMessage](/dotnet/api/azure.storage.queues.queueclient.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. Your code calls `DeleteMessage` right after the message has been processed.
323+
De-queue a message from a queue in two steps. When you call [ReceiveMessages](/dotnet/api/azure.storage.queues.queueclient.receivemessages), you get the next message in a queue. A message returned from `ReceiveMessages` becomes invisible to any other code reading messages from this queue. By default, this message stays invisible for 30 seconds. To finish removing the message from the queue, you must also call [DeleteMessage](/dotnet/api/azure.storage.queues.queueclient.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. Your code calls `DeleteMessage` right after the message has been processed.
390324

391-
```csharp
392-
// Get the connection string from app settings
393-
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
394-
395-
// Instantiate a QueueClient which will be used to create and manipulate the queue
396-
QueueClient queueClient = new QueueClient(connectionString, "myqueue");
397-
398-
if (queueClient.Exists())
399-
{
400-
// Get the next message
401-
QueueMessage[] retrievedMessage = queueClient.ReceiveMessages();
402-
403-
//Process the message in less than 30 seconds, and then...
404-
405-
// Delete the message
406-
queueClient.DeleteMessage(retrievedMessage[0].MessageId, retrievedMessage[0].PopReceipt);
407-
}
408-
```
325+
:::code language="csharp" source="~/azure-storage-snippets/queues/howto/dotnet/dotnet-v12/QueueBasics.cs" id="snippet_DequeueMessage":::
409326

410327
# [\.NET v11](#tab/dotnetv11)
411328

@@ -437,35 +354,7 @@ This example shows how to use the Async-Await pattern with common Queue storage
437354

438355
# [\.NET v12](#tab/dotnet)
439356

440-
```csharp
441-
// Create the queue if it doesn't already exist
442-
await queueClient.CreateIfNotExistsAsync();
443-
444-
if (await queueClient.ExistsAsync())
445-
{
446-
Console.WriteLine("Queue '{0}' Created", queueClient.Name);
447-
}
448-
else
449-
{
450-
Console.WriteLine("Queue '{0}' Exists", queueClient.Name);
451-
}
452-
453-
// Async enqueue the message
454-
await queueClient.SendMessageAsync("Hello, World");
455-
Console.WriteLine("Message added");
456-
457-
// Async receive the message
458-
QueueMessage[] retrievedMessage = await queueClient.ReceiveMessagesAsync();
459-
Console.WriteLine("Retrieved message with content '{0}'", retrievedMessage[0].MessageText);
460-
461-
// Async delete the message
462-
await queueClient.DeleteMessageAsync(retrievedMessage[0].MessageId, retrievedMessage[0].PopReceipt);
463-
Console.WriteLine("Deleted message");
464-
465-
// Async delete the queue
466-
await queueClient.DeleteAsync();
467-
Console.WriteLine("Deleted queue");
468-
```
357+
:::code language="csharp" source="~/azure-storage-snippets/queues/howto/dotnet/dotnet-v12/QueueBasics.cs" id="snippet_AsyncQueue":::
469358

470359
# [\.NET v11](#tab/dotnetv11)
471360

@@ -506,30 +395,7 @@ There are two ways you can customize message retrieval from a queue. First, you
506395

507396
The following code example uses the [ReceiveMessages](/dotnet/api/azure.storage.queues.queueclient.receivemessages) method to get 20 messages in one call. Then it processes each message using a `foreach` loop. It also sets the invisibility timeout to five minutes for each message. 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.
508397

509-
```csharp
510-
// Get the connection string from app settings
511-
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
512-
513-
// Instantiate a QueueClient which will be used to create and manipulate the queue
514-
QueueClient queueClient = new QueueClient(connectionString, "myqueue");
515-
516-
if (queueClient.Exists())
517-
{
518-
// Receive and process 20 messages
519-
QueueMessage[] receivedMessages = queueClient.ReceiveMessages(20, TimeSpan.FromMinutes(5));
520-
521-
foreach (QueueMessage message in receivedMessages)
522-
{
523-
// Display the message
524-
Console.WriteLine(message.MessageText);
525-
526-
//Process the message in less than 30 seconds, and then...
527-
528-
// Delete the message
529-
queueClient.DeleteMessage(message.MessageId, message.PopReceipt);
530-
}
531-
}
532-
```
398+
:::code language="csharp" source="~/azure-storage-snippets/queues/howto/dotnet/dotnet-v12/QueueBasics.cs" id="snippet_DequeueMessages":::
533399

534400
# [\.NET v11](#tab/dotnetv11)
535401

@@ -562,24 +428,7 @@ foreach (CloudQueueMessage message in queue.GetMessages(20, TimeSpan.FromMinutes
562428

563429
You can get an estimate of the number of messages in a queue. The [GetProperties](/dotnet/api/azure.storage.queues.queueclient.getproperties) method asks the Queue service to retrieve the queue properties, including the message count. The [ApproximateMessagesCount](/dotnet/api/azure.storage.queues.models.queueproperties.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.
564430

565-
```csharp
566-
// Get the connection string from app settings
567-
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
568-
569-
// Instantiate a QueueClient which will be used to create and manipulate the queue
570-
QueueClient queueClient = new QueueClient(connectionString, "myqueue");
571-
572-
if (queueClient.Exists())
573-
{
574-
QueueProperties properties = queueClient.GetProperties();
575-
576-
// Retrieve the cached approximate message count.
577-
int cachedMessagesCount = properties.ApproximateMessagesCount;
578-
579-
// Display number of messages.
580-
Console.WriteLine("Number of messages in queue: " + cachedMessagesCount);
581-
}
582-
```
431+
:::code language="csharp" source="~/azure-storage-snippets/queues/howto/dotnet/dotnet-v12/QueueBasics.cs" id="snippet_GetQueueLength":::
583432

584433
# [\.NET v11](#tab/dotnetv11)
585434

@@ -614,19 +463,7 @@ Console.WriteLine("Number of messages in queue: " + cachedMessageCount);
614463

615464
To delete a queue and all the messages contained in it, call the [Delete](/dotnet/api/azure.storage.queues.queueclient.delete) method on the queue object.
616465

617-
```csharp
618-
// Get the connection string from app settings
619-
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
620-
621-
// Instantiate a QueueClient which will be used to create and manipulate the queue
622-
QueueClient queueClient = new QueueClient(connectionString, "myqueue");
623-
624-
if (queueClient.Exists())
625-
{
626-
// Delete the queue
627-
queueClient.Delete();
628-
}
629-
```
466+
:::code language="csharp" source="~/azure-storage-snippets/queues/howto/dotnet/dotnet-v12/QueueBasics.cs" id="snippet_DeleteQueue":::
630467

631468
# [\.NET v11](#tab/dotnetv11)
632469

0 commit comments

Comments
 (0)