异步/等待似乎挂在网络方法内

时间:2019-05-29 03:02:26

标签: c# asynchronous sendgrid-api-v3

在C#中,我有一个Web服务,其操作结果定义如下:

OperationResult<createAccountResponse> CreateAccount(string token, createAccountServiceModel model);

在该方法中,我调用另一个带有签名的方法,该签名指示该方法是异步的,例如:

var sendEmailInvite = this.SendExhibitorInviteEmailAsync(new ExhibitorInviteEmailPartialRequest()
{
    CompanyId = company.CompanyID,
    EventId = event.EventID
});

在SendExhibitorInviteEmailAsync内部,我等待一个也被标记为异步的方法。这就是该方法(为简洁起见)

public async Task<ExhibitorInviteEmailResponse> SendExhibitorInviteEmailAsync(ExhibitorInviteEmailResolvedRequest request)
{
    ExhibitorInviteEmailResponse response = null;
    try
    {
        response = new ExhibitorInviteEmailResponse();
        var apiKey = "snip";
        var client = new SendGridClient(apiKey);
        var msg = new SendGridMessage();
        msg.SetFrom(new EmailAddress("noreply@domain.com", "Display name"));
        msg.AddTo(new EmailAddress(request.EmailAddress, request.AccountOwnerFirstName));
        msg.SetTemplateId("snipped");
\
        msg.SetTemplateData(dynamicTemplateData);
        await client.SendEmailAsync(msg);

    }
    catch (Exception ex)
    {
        response = new ExhibitorInviteEmailResponse
        {
            Success = false,
            Error = true,
            ErrorMessage = ex.Message
        };
    }
    return response;
}

如果要发送电子邮件(json中的flag字段),那么我将开始处理该电子邮件;如果不打算发送电子邮件,则整个方法将花费大约一秒钟的时间。 / p>

我遇到的问题是,当我从Postman或Postman生成的C#运行此方法时,似乎用于发送电子邮件的异步代码导致请求的持续时间为30秒以上-似乎是不等待电子邮件发送?当我通过浏览器运行此程序时,它会在1-2秒内立即生效。

使用Postman和async时,建议的流程是什么?所有内部方法的父母也都必须等待吗?

0 个答案:

没有答案