简体   繁体   English

使用等待方法的返回值

[英]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 等)中的一种方法中使用了ResultWait (或调用您的代码的东西)。 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.

相关问题 反应组件没有从异步方法获得回报 - react component not getting a return from asyncronous method 如何使用 on() firebase 数据库方法返回值给调用 function? - how to use on() firebase database method to return value to the calling function? 从 firebase 读取时返回 null 值 - return null value when reading from firebase 我如何使用一种方法从 Firebase 实时数据库中获取单个值 - How can i fetch single value from Firebase real time database using a method Laravel Nova:头像字段使用 S3 磁盘但返回 0 值 - Laravel Nova : Avatar Fields using S3 disk but return 0 value 从汇编过程写入返回值时出现意外的页面错误 - Unexpected page fault when writing return value from assembly procedure BigQuery:从 Group By 中的不同组返回第一个值 - BigQuery: Return First Value from Different Groups in a Group By Firebase中的within.on().once()调用返回一个值,供in.then()使用 - Return a value from within .on() .once() call in Firebase for use in .then() 当值不存在时,DynamoDBMapper 的查询方法返回什么? - What does DynamoDBMapper's query method return when value doesn't exist? 如何从 Python 中的 BigQuery 返回 boolean 值 - How to return boolean value from BigQuery in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM