Skip to content

Commit 9e8d545

Browse files
zedy-wjBillWagner
andauthored
Update F# on Azure Docs - Queue Storage (#24465)
* Updating Queue Storage to the new SDKs. * Update reference * test path * modify format * Update the doc in queue-storage * remove invalid changes * Updating some variables' name and removing invalid changes. * remove repeat comments after the 3-line header block for each section * omit the new when creating queueClient. * Update ReadMe file * Update Readme file * Remove en-us in link * Update docs/fsharp/using-fsharp-on-azure/queue-storage.md Co-authored-by: Wenjie Yu <v-wenjyu@microsoft.com> Co-authored-by: Bill Wagner <wiwagn@microsoft.com>
1 parent 8025e4c commit 9e8d545

File tree

2 files changed

+55
-72
lines changed

2 files changed

+55
-72
lines changed

docs/fsharp/using-fsharp-on-azure/queue-storage.md

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,67 +30,59 @@ Next, use a [package manager](package-management.md) such as [Paket](https://fsp
3030

3131
Add the following `open` statements to the top of the `queues.fsx` file:
3232

33-
[!code-fsharp[QueueStorage](~/samples/snippets/fsharp/azure/queue-storage.fsx#L1-L3)]
33+
[!code-fsharp[QueueStorage](../../../samples/snippets/fsharp/azure/queue-storage.fsx#L1-L3)]
3434

3535
### Get your connection string
3636

3737
You'll need an Azure Storage connection string for this tutorial. For more information about connection strings, see [Configure Storage Connection Strings](/azure/storage/storage-configure-connection-string).
3838

3939
For the tutorial, you'll enter your connection string in your script, like this:
4040

41-
[!code-fsharp[QueueStorage](~/samples/snippets/fsharp/azure/queue-storage.fsx#L9-L9)]
41+
[!code-fsharp[QueueStorage](../../../samples/snippets/fsharp/azure/queue-storage.fsx#L9-L9)]
4242

4343
However, this is **not recommended** for real projects. 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. You can regenerate your key using the Azure portal if you believe it may have been compromised.
4444

4545
For real applications, the best way to maintain your storage connection string is in a configuration file. To fetch the connection string from a configuration file, you can do this:
4646

47-
[!code-fsharp[QueueStorage](~/samples/snippets/fsharp/azure/queue-storage.fsx#L11-L13)]
47+
[!code-fsharp[QueueStorage](../../../samples/snippets/fsharp/azure/queue-storage.fsx#L11-L13)]
4848

4949
Using Azure Configuration Manager is optional. You can also use an API such as the .NET Framework's `ConfigurationManager` type.
5050

51-
### Parse the connection string
52-
53-
To parse the connection string, use:
54-
55-
[!code-fsharp[QueueStorage](~/samples/snippets/fsharp/azure/queue-storage.fsx#L19-L20)]
56-
57-
This will return a `CloudStorageAccount`.
58-
5951
### Create the Queue service client
6052

61-
The `CloudQueueClient` class enables you to retrieve queues stored in Queue storage. Here's one way to create the service client:
53+
The `QueueClient` class enables you to retrieve queues stored in Queue storage. Here's one way to create the client:
6254

63-
[!code-fsharp[QueueStorage](~/samples/snippets/fsharp/azure/queue-storage.fsx#L26-L26)]
55+
[!code-fsharp[QueueStorage](../../../samples/snippets/fsharp/azure/queue-storage.fsx#L20-L20)]
6456

6557
Now you are ready to write code that reads data from and writes data to Queue storage.
6658

6759
## Create a queue
6860

6961
This example shows how to create a queue if it doesn't already exist:
7062

71-
[!code-fsharp[QueueStorage](~/samples/snippets/fsharp/azure/queue-storage.fsx#L32-L36)]
63+
[!code-fsharp[QueueStorage](../../../samples/snippets/fsharp/azure/queue-storage.fsx#L26-L26)]
7264

7365
## Insert a message into a queue
7466

7567
To insert a message into an existing queue, first create a new
76-
`CloudQueueMessage`. Next, call the `AddMessage` method. A
77-
`CloudQueueMessage` can be created from either a string (in UTF-8
68+
Message. Next, call the `SendMessage` method. A
69+
Message can be created from either a string (in UTF-8
7870
format) or a `byte` array, like this:
7971

80-
[!code-fsharp[QueueStorage](~/samples/snippets/fsharp/azure/queue-storage.fsx#L42-L44)]
72+
[!code-fsharp[QueueStorage](../../../samples/snippets/fsharp/azure/queue-storage.fsx#L32-L33)]
8173

8274
## Peek at the next message
8375

8476
You can peek at the message in the front of a queue, without removing it
8577
from the queue, by calling the `PeekMessage` method.
8678

87-
[!code-fsharp[QueueStorage](~/samples/snippets/fsharp/azure/queue-storage.fsx#L50-L52)]
79+
[!code-fsharp[QueueStorage](../../../samples/snippets/fsharp/azure/queue-storage.fsx#L39-L40)]
8880

8981
## Get the next message for processing
9082

91-
You can retrieve the message at the front of a queue for processing by calling the `GetMessage` method.
83+
You can retrieve the message at the front of a queue for processing by calling the `ReceiveMessage` method.
9284

93-
[!code-fsharp[QueueStorage](~/samples/snippets/fsharp/azure/queue-storage.fsx#L58-L59)]
85+
[!code-fsharp[QueueStorage](../../../samples/snippets/fsharp/azure/queue-storage.fsx#L46-L46)]
9486

9587
You later indicate successful processing of the message by using `DeleteMessage`.
9688

@@ -109,60 +101,65 @@ you would keep a retry count as well, and if the message is retried more
109101
than some number of times, you would delete it. This protects against a message
110102
that triggers an application error each time it is processed.
111103

112-
[!code-fsharp[QueueStorage](~/samples/snippets/fsharp/azure/queue-storage.fsx#L65-L69)]
104+
[!code-fsharp[QueueStorage](../../../samples/snippets/fsharp/azure/queue-storage.fsx#L52-L56)]
113105

114106
## De-queue the next message
115107

116108
Your code de-queues a message from a queue in two steps. When you call
117-
`GetMessage`, you get the next message in a queue. A message returned
118-
from `GetMessage` becomes invisible to any other code reading messages
109+
`ReceiveMessage`, you get the next message in a queue. A message returned
110+
from `ReceiveMessage` becomes invisible to any other code reading messages
119111
from this queue. By default, this message stays invisible for 30
120112
seconds. To finish removing the message from the queue, you must also
121113
call `DeleteMessage`. This two-step process of removing a message
122114
assures that if your code fails to process a message due to hardware or
123115
software failure, another instance of your code can get the same message
124116
and try again. Your code calls `DeleteMessage` right after the message
125117
has been processed.
118+
All of the Queue methods we've shown so far have `Async` alternatives.
126119

127-
[!code-fsharp[QueueStorage](~/samples/snippets/fsharp/azure/queue-storage.fsx#L75-L76)]
120+
[!code-fsharp[QueueStorage](../../../samples/snippets/fsharp/azure/queue-storage.fsx#L62-L63)]
128121

129122
## Use Async workflows with common Queue storage APIs
130123

131124
This example shows how to use an async workflow with common Queue storage APIs.
132125

133-
[!code-fsharp[QueueStorage](~/samples/snippets/fsharp/azure/queue-storage.fsx#L82-L91)]
126+
[!code-fsharp[QueueStorage](../../../samples/snippets/fsharp/azure/queue-storage.fsx#L69-L78)]
134127

135128
## Additional options for de-queuing messages
136129

137130
There are two ways you can customize message retrieval from a queue.
138131
First, you can get a batch of messages (up to 32). Second, you can set a
139132
longer or shorter invisibility timeout, allowing your code more or less
140133
time to fully process each message. The following code example uses
141-
`GetMessages` to get 20 messages in one call and then processes
134+
`ReceiveMessages` to get 20 messages in one call and then processes
142135
each message. It also sets the invisibility timeout to five minutes for
143136
each message. The 5 minutes starts for all messages at the same
144-
time, so after 5 minutes have passed since the call to `GetMessages`, any
137+
time, so after 5 minutes have passed since the call to `ReceiveMessages`, any
145138
messages that have not been deleted will become visible again.
146139

147-
[!code-fsharp[QueueStorage](~/samples/snippets/fsharp/azure/queue-storage.fsx#L97-L99)]
140+
[!code-fsharp[QueueStorage](../../../samples/snippets/fsharp/azure/queue-storage.fsx#L84-L86)]
148141

149142
## Get the queue length
150143

151-
You can get an estimate of the number of messages in a queue. The `FetchAttributes` method asks the Queue service to retrieve the queue attributes, including the message count. The `ApproximateMessageCount` property returns the last value retrieved by the `FetchAttributes` method, without calling the Queue service.
144+
You can get an estimate of the number of messages in a queue. The `GetProperties` method asks the Queue service to retrieve the queue attributes, including the message count. The `ApproximateMessagesCount` property returns the last value retrieved by the `GetProperties` method.
152145

153-
[!code-fsharp[QueueStorage](~/samples/snippets/fsharp/azure/queue-storage.fsx#L105-L106)]
146+
[!code-fsharp[QueueStorage](../../../samples/snippets/fsharp/azure/queue-storage.fsx#L92-L93)]
154147

155148
## Delete a queue
156149

157150
To delete a queue and all the messages contained in it, call the
158151
`Delete` method on the queue object.
159152

160-
[!code-fsharp[QueueStorage](~/samples/snippets/fsharp/azure/queue-storage.fsx#L112-L113)]
153+
[!code-fsharp[QueueStorage](../../../samples/snippets/fsharp/azure/queue-storage.fsx#L99-L99)]
154+
155+
## Note
156+
157+
If you're migrating from the old libraries, they Base64 encoded messages by default but the new libraries do not because it's more performant.
158+
See [MessageEncoding](https://docs.microsoft.com/dotnet/api/azure.storage.queues.queueclientoptions.messageencoding?view=azure-dotnet#Azure_Storage_Queues_QueueClientOptions_MessageEncoding) for how to set that up.
161159

162160
## Next steps
163161

164-
Now that you've learned the basics of Queue storage, follow these links
165-
to learn about more complex storage tasks.
162+
Now that you've learned the basics of Queue storage, follow these links to learn about more complex storage tasks.
166163

167164
- [Azure Storage APIs for .NET](/dotnet/api/overview/azure/storage)
168165
- [Azure Storage Type Provider](https://github.com/fsprojects/AzureStorageTypeProvider)
Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
open Microsoft.Azure // Namespace for CloudConfigurationManager
2-
open Microsoft.Azure.Storage // Namespace for CloudStorageAccount
3-
open Microsoft.Azure.Storage.Queue // Namespace for Queue storage types
1+
open Azure.Storage.Queues // Namespace for Queue storage types
2+
open System
3+
open System.Text
44

55
//
66
// Get your connection string.
@@ -12,102 +12,88 @@ let storageConnString = "..." // fill this in from your storage account
1212
let storageConnString =
1313
CloudConfigurationManager.GetSetting("StorageConnectionString")
1414
*)
15-
//
16-
// Parse the connection string.
17-
//
18-
19-
// Parse the connection string and return a reference to the storage account.
20-
let storageAccount = CloudStorageAccount.Parse(storageConnString)
2115

2216
//
2317
// Create the Queue Service client.
2418
//
2519

26-
let queueClient = storageAccount.CreateCloudQueueClient()
20+
let queueClient = QueueClient(storageConnString, "myqueue")
2721

2822
//
2923
// Create a queue.
3024
//
3125

32-
// Retrieve a reference to a container.
33-
let queue = queueClient.GetQueueReference("myqueue")
34-
35-
// Create the queue if it doesn't already exist
36-
queue.CreateIfNotExists()
26+
queueClient.CreateIfNotExists()
3727

3828
//
3929
// Insert a message into a queue.
4030
//
4131

42-
// Create a message and add it to the queue.
43-
let message = new CloudQueueMessage("Hello, World")
44-
queue.AddMessage(message)
32+
queueClient.SendMessage("Hello, World") // Insert a String message into a queue
33+
queueClient.SendMessage(BinaryData.FromBytes(Encoding.UTF8.GetBytes("Hello, World"))) // Insert a BinaryData message into a queue
4534

4635
//
4736
// Peek at the next message.
4837
//
4938

50-
// Peek at the next message.
51-
let peekedMessage = queue.PeekMessage()
52-
let msgAsString = peekedMessage.AsString
39+
let peekedMessage = queueClient.PeekMessage()
40+
let messageContents = peekedMessage.Value.Body.ToString()
5341

5442
//
5543
// Get the next message.
5644
//
5745

58-
// Get the next message. Successful processing must be indicated via DeleteMessage later.
59-
let retrieved = queue.GetMessage()
46+
let updateMessage = queueClient.ReceiveMessage().Value
6047

6148
//
6249
// Change the contents of a retrieved message.
6350
//
6451

65-
// Update the message contents and set a new timeout.
66-
retrieved.SetMessageContent("Updated contents.")
67-
queue.UpdateMessage(retrieved,
68-
TimeSpan.FromSeconds(60.0),
69-
MessageUpdateFields.Content ||| MessageUpdateFields.Visibility)
52+
queueClient.UpdateMessage(
53+
updateMessage.MessageId,
54+
updateMessage.PopReceipt,
55+
"Updated contents.",
56+
TimeSpan.FromSeconds(60.0))
7057

7158
//
7259
// De-queue the next message, indicating successful processing
7360
//
7461

75-
// Process the message in less than 30 seconds, and then delete the message.
76-
queue.DeleteMessage(retrieved)
62+
let deleteMessage = queueClient.ReceiveMessage().Value
63+
queueClient.DeleteMessage(deleteMessage.MessageId, deleteMessage.PopReceipt)
7764

7865
//
7966
// Use Async-Await pattern with common Queue storage APIs.
8067
//
8168

8269
async {
83-
let! exists = queue.CreateIfNotExistsAsync() |> Async.AwaitTask
70+
let! exists = queueClient.CreateIfNotExistsAsync() |> Async.AwaitTask
8471

85-
let! retrieved = queue.GetMessageAsync() |> Async.AwaitTask
72+
let! delAsyncMessage = queueClient.ReceiveMessageAsync() |> Async.AwaitTask
8673

8774
// ... process the message here ...
8875

8976
// Now indicate successful processing:
90-
do! queue.DeleteMessageAsync(retrieved) |> Async.AwaitTask
77+
queueClient.DeleteMessageAsync(delAsyncMessage.Value.MessageId, delAsyncMessage.Value.PopReceipt) |> Async.AwaitTask
9178
}
9279

9380
//
9481
// Additional options for de-queuing messages.
9582
//
9683

97-
for msg in queue.GetMessages(20, Nullable(TimeSpan.FromMinutes(5.))) do
84+
for dequeueMessage in queueClient.ReceiveMessages(20, Nullable(TimeSpan.FromMinutes(5.))).Value do
9885
// Process the message here.
99-
queue.DeleteMessage(msg)
86+
queueClient.DeleteMessage(dequeueMessage.MessageId, dequeueMessage.PopReceipt)
10087

10188
//
10289
// Get the queue length.
10390
//
10491

105-
queue.FetchAttributes()
106-
let count = queue.ApproximateMessageCount.GetValueOrDefault()
92+
let properties = queueClient.GetProperties().Value
93+
let count = properties.ApproximateMessagesCount
10794

10895
//
10996
// Delete a queue.
11097
//
11198

112-
// Delete the queue.
113-
queue.Delete()
99+
queueClient.DeleteIfExists()

0 commit comments

Comments
 (0)