如何同步执行此操作?

时间:2019-08-22 23:27:04

标签: c# .net rest async-await ringcentral

我一直在研究RingCentral API的示例代码。我想同步执行以下代码。

我已经开始熟悉“ ContinueWith”。

//From API sample
using (var rc = new RestClient("clientID", "clientSecret", "serverURL"))
    {
        await rc.Authorize("username", "extension", "password");
        var result = await rc.Restapi(apiVersion).Account(accountId).Recording(recordingId).Get();
    }
//My first attempt (I am unable to get the value of the result)
using (var rc = new RestClient("clientID", "clientSecret", "serverURL"))
    {
        rc.Authorize("username", "extension", "password").ContinueWith(rc.Restapi(apiVersion).Account(accountId).Recording(recordingId).Get());
    }

1 个答案:

答案 0 :(得分:1)

要使其同步,请删除所有await,然后像这样使用.Wait().Result

rc.Authorize("username", "extension", "password").Wait();
var result = rc.Restapi(apiVersion).Account(accountId).Recording(recordingId).Get().Result;

但是您实际上 应该 正在使用异步/等待来调用API。例如,使用C# 7.1 even Main() can be declared async,意味着所有后续方法调用都可以在整个代码库中使用async / await。