gpt4 book ai didi

azure - 在异步方法中使用 QueueClient.OnMessage 是否有意义?

转载 作者:行者123 更新时间:2023-12-03 00:12:47 26 4
gpt4 key购买 nike

我正在从异步方法ConfigureConnectionString 调用异步方法InsertOperation。我是否正确使用了 client.OnMessage 调用?我想异步处理队列中的消息,然后将它们存储到队列存储中。

private static async void ConfigureConnectionString()
{
var connectionString =
"myconnstring";
var queueName = "myqueue";

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

CloudTable table = tableClient.GetTableReference("test");
table.CreateIfNotExists();
Stopwatch sw = Stopwatch.StartNew();
await Task.Run(() => InsertOperation(connectionString, queueName, table));
sw.Stop();
Console.WriteLine("ElapsedTime " + sw.Elapsed.TotalMinutes + " minutes.");

}


private static async Task InsertOperation(string connectionString, string queueName, CloudTable table)
{

var client = QueueClient.CreateFromConnectionString(connectionString, queueName);
client.OnMessage(message =>
{

var bodyJson = new StreamReader(message.GetBody<Stream>(), Encoding.UTF8).ReadToEnd();
var myMessage = JsonConvert.DeserializeObject<VerifyVariable>(bodyJson);

Console.WriteLine();

var VerifyVariableEntityObject = new VerifyVariableEntity()
{
ConsumerId = myMessage.ConsumerId,
Score = myMessage.Score,
PartitionKey = myMessage.ConsumerId,
RowKey = myMessage.Score
};
});
}

最佳答案

OnMessageAsync method提供异步编程模型,它使我们能够异步处理消息。

client.OnMessageAsync(message =>
{
return Task.Factory.StartNew(() => ProcessMessage(message));
//you could perofrm table and queue storage in ProcessMessage method
}, options);

关于azure - 在异步方法中使用 QueueClient.OnMessage 是否有意义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40894573/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com