csharp Azure存储队列
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp Azure存储队列相关的知识,希望对你有一定的参考价值。
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("storageConnectionString");
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
// Create a queue if it doesn't exist already
CloudQueue queue = queueClient.GetQueueReference(queueName);
\!h await queue.CreateIfNotExistsAsync();
// Insert a message into the queue
\!h await queue.AddMessageAsync(new CloudQueueMessage("Hello World!"));
// Peek at the message in the front of a queue without removing it from the queue
\!h CloudQueueMessage peekedMessage = await queue.PeekMessageAsync();
Console.WriteLine("The peeked message is: {0}", peekedMessage.AsString);
// You de-queue a message in two steps. Call GetMessage at which point the message becomes invisible to any other code reading messages
// from this queue for a default period of 30 seconds. To finish removing the message from the queue, you call DeleteMessage.
\!h CloudQueueMessage message = await queue.GetMessageAsync();
// Process message... then delete
\!h await queue.DeleteMessageAsync(message);
// Update an enqueued message
\!h await queue.AddMessageAsync(new CloudQueueMessage("Hello World Again!"));
\!h CloudQueueMessage message = await queue.GetMessageAsync();
message.SetMessageContent2("Updated contents.", false);
\!h await queue.UpdateMessageAsync(
message,
TimeSpan.Zero, // Make the update visible immediately
MessageUpdateFields.Content |
MessageUpdateFields.Visibility);
// The FetchAttributes method asks the Queue service to retrieve the queue attributes, including an approximation of message count
queue.FetchAttributes();
int? cachedMessageCount = queue.ApproximateMessageCount;
// Dequeue 21 messages (up to 32 at a time) and set visibility timeout to 5 minutes
\!h await queue.GetMessagesAsync(21, TimeSpan.FromMinutes(5), null, null)
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("storageConnectionString");
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
// Create a queue if it doesn't exist already
CloudQueue queue = queueClient.GetQueueReference(queueName);
\!h await queue.CreateIfNotExistsAsync();
// List queues
// List the queues for this storage account
QueueContinuationToken token = null;
List<CloudQueue> cloudQueueList = new List<CloudQueue>();
do
{
\!h QueueResultSegment segment = await cloudQueueClient.ListQueuesSegmentedAsync(baseQueueName, token);
token = segment.ContinuationToken;
cloudQueueList.AddRange(segment.Results);
}
while (token != null);
// Manage the properties of the Queue service
\!h ServiceProperties props = await queueClient.GetServicePropertiesAsync();
props.Logging.LoggingOperations = LoggingOperations.Read | LoggingOperations.Write;
props.Logging.RetentionDays = 5;
// etc.
\!h await queueClient.SetServicePropertiesAsync(props);
// Statistics (request from secondary only?)
queueClient.DefaultRequestOptions.LocationMode = LocationMode.SecondaryOnly;
\!h ServiceStats stats = await queueClient.GetServiceStatsAsync();
// Last sync time
stats.GeoReplication.LastSyncTime;
// Status
stats.GeoReplication.Status;
// Manage queue metadata
CloudQueue queue = cloudQueueClient.GetQueueReference(queueName);
// Set queue metadata
queue.Metadata.Add("key1", "value1");
queue.Metadata.Add("key2", "value2");
\!h await queue.CreateIfNotExistsAsync();
// Fetch queue attributes - in this case this call is not need but is included for demo purposes
\!h await queue.FetchAttributesAsync();
foreach (var keyValue in queue.Metadata)
{
Console.WriteLine("{0}: {1}", keyValue.Key, keyValue.Value);
}
// Set queue permissions
SharedAccessQueuePolicy accessQueuePolicy = new SharedAccessQueuePolicy
{
SharedAccessStartTime = new DateTimeOffset(DateTime.Now),
SharedAccessExpiryTime = new DateTimeOffset(DateTime.Now.AddMinutes(10)),
Permissions = SharedAccessQueuePermissions.Update
};
QueuePermissions permissions = new QueuePermissions();
permissions.SharedAccessPolicies.Add("key1", accessQueuePolicy);
\!h await queue.SetPermissionsAsync(permissions);
// Get queue permissions
\!h permissions = await queue.GetPermissionsAsync();
foreach (var keyValue in permissions.SharedAccessPolicies)
{
Console.WriteLine(" {0}:", keyValue.Key);
Console.WriteLine(" permissions: {0}:", keyValue.Value.Permissions);
Console.WriteLine(" start time: {0}:", keyValue.Value.SharedAccessStartTime);
Console.WriteLine(" expiry time: {0}:", keyValue.Value.SharedAccessExpiryTime);
}
以上是关于csharp Azure存储队列的主要内容,如果未能解决你的问题,请参考以下文章