[英]Using return value from awaited method
I'm trying to understand async
methods and await
, I've got this simple example:我试图理解async
方法和await
,我有这个简单的例子:
using (var client = new AmazonSQSClient())
{
var sqsRequest = new SendMessageRequest()
{
MessageBody = JsonConvert.SerializeObject(callProcessingRequest),
QueueUrl = "https://sqs.eu-west-2.amazonaws.com/*****6014/W*****g"
};
LoggingHelper.Log(LoggingLevel.INFO, "Calling SQS", context);
var sqsResponse = await client.SendMessageAsync(sqsRequest);
LoggingHelper.Log(LoggingLevel.DEBUG,
JsonConvert.SerializeObject(sqsResponse), context)
}
When I run this, the logging of sqsResponse
never happens, however if I change当我运行它时, sqsResponse
的记录永远不会发生,但是如果我改变
var sqsResponse = await client.SendMessageAsync(sqsRequest);
to到
var sqsResponse = client.SendMessageAsync(sqsRequest).Result;
Then it works as expected.然后它按预期工作。
With it being an async
method I guess I should be using await
with it, but not sure why it's not working.由于它是一种async
方法,我想我应该对它使用await
,但不确定为什么它不起作用。
EDIT: Whole method as requested.编辑:按要求的整个方法。 Adding .ConfigureAwait(false)
didn't help.添加.ConfigureAwait(false)
没有帮助。
public static async Task ProcessOutstandingDialplanItems()
{
var context = new Context() { CurrentHandler = "PBXCallbacksLambdaFunction" };
var callProcessingRequest = new CallProcessingRequest()
{
context = context,
MethodToInvoke = "ProcessOutstandingDialPlanItems"
};
try
{
using (var client = new AmazonSQSClient())
{
var sqsRequest = new SendMessageRequest()
{
MessageBody = JsonConvert.SerializeObject(callProcessingRequest),
QueueUrl = "https://sqs.eu-west-2.amazonaws.com/XXX6014/WXXXg"
};
LambdaLogger.Log("Calling SQS");
var sqsResponse = await client.SendMessageAsync(sqsRequest)
.ConfigureAwait(false);
//var sqsResponse = client.SendMessageAsync(sqsRequest).Result;
LambdaLogger.Log(JsonConvert.SerializeObject(sqsResponse));
}
}
catch (Exception x)
{
LambdaLogger.Log(x.Message);
}
}
From AWS Logging .NET :来自AWS 日志 .NET :
AWS Lambda AWS Lambda
These packages batch logging messages in a queue and send messages to CloudWatch Logs using a background thread.这些包在队列中批量记录消息,并使用后台线程将消息发送到 CloudWatch Logs。 The use of the background thread means that the messages are not guaranteed to be delivered when used in AWS Lambda .使用后台线程意味着在 AWS Lambda 中使用时不能保证消息被传递。 The reason is because the background thread will be frozen once a Lambda event is processed and may not ever be unfrozen if more Lambda events are not received for some time.原因是一旦 Lambda 事件被处理,后台线程将被冻结,如果一段时间内没有收到更多 Lambda 事件,后台线程可能永远不会解冻。
When using Lambda it is recommended to use either the ILambdaContext.Logger.LogLine or the Amazon.Lambda.Logging.AspNetCore package.使用 Lambda 时,建议使用 ILambdaContext.Logger.LogLine 或 Amazon.Lambda.Logging.AspNetCore package。
My guess would be that you are already using Result
or Wait
(or something which is calling your code) in one of the method before in application which has SynchronizationContext
(classic ASP.NET, UI apps like WPF, WinForms, etc.).我的猜测是,您已经在具有SynchronizationContext
的应用程序(经典 ASP.NET、UI 应用程序,如 WPF、WinForms 等)中的一种方法中使用了Result
或Wait
(或调用您的代码的东西)。 That is a "good" way to end up in deadlock.这是陷入僵局的“好”方式。 One thing you can try - adding ConfigureAwait(false)
to the call:您可以尝试一件事 - 将ConfigureAwait(false)
添加到调用中:
var sqsResponse = await client.SendMessageAsync(sqsRequest).ConfigureAwait(false);
But much better course of action - Don't Block on Async Code .但更好的做法是—— 不要阻塞异步代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.