-1

I've been looking at sample code from RingCentral API. I'd like to execute the following code synchronously.

I've begun familiarizing myself with '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 Answers1

1

To make it synchronous, remove all await, and use .Wait() and .Result like this:

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

However you really should be using async/await to call an API. For example, with C# 7.1 even Main() can be declared async, meaning that all subsequent method calls can use async/await throughout your codebase.

BlueSky
  • 1,449
  • 1
  • 16
  • 22
  • 3
    `async Main` is just [syntactic sugar](https://blogs.msdn.microsoft.com/mazhou/2017/05/30/c-7-series-part-2-async-main/) that is compiled to a synchronous blocking `Main`. – Theodor Zoulias Aug 23 '19 at 07:50