I have created a C# Restsharp client to make queries towards my API using synchronous calls. The synchronous calls work as they should, however my asynchronous calls never return anything. The await operator should suspend the evaluation of the enclosing async method until the asynchronous operation represented by its operand completes. When the asynchronous operation completes, the await operator returns the result of the operation. However, in my case, this never happends. I have searched through the whole of stack overflow now, but I cannot figure out why my async calls won't return anything. Can anyone spot what may cause the issues and how I can fix it? This is my setup:
The client side method:
public class Objects{
private readonly IRestClient _restClient;
public Objects(IRestClient restClient)
{
_restClient = restClient;
}
public async Task< List<ObjectGet> CreateObjects(List<ObjectBase>> objects, Boolean createCdfFile = true)
{
var request = new RestRequest("/objects/", Method.POST) { RequestFormat = DataFormat.Json };
var objectsJson = JsonConvert.SerializeObject(objects, Formatting.Indented);
request.AddParameter("create_cdf_file", createCdfFile, ParameterType.QueryString)
.AddParameter("application/json", objectsJson, ParameterType.RequestBody);
IRestResponse<List<ObjectGet>> response = await _restClient.ExecuteAsync<List<ObjectGet>>(request);
return response.Data;
}
}
My synchronous call works:
var obj = _restClient.FetchAllObjects(filter).GetAwaiter().GetResult();
returns : System.Collections.Generic.List1[RestsharpApiClient.model.ObjectWithRelationships]
My asynchronous call executes, but..
var obj = _restClient.FetchAllObjects(filter);
never returns anything:
However, if run the async call before the synchronous call in sequence, they both return the right result:
var obj = _restClient.FetchAllObjects(filter);
var objAsync = _restClient.FetchAllObjects(filter).GetAwaiter().GetResult();
returns:
System.Collections.Generic.List1[RestsharpApiClient.model.ObjectWithRelationships] , System.Collections.Generic.List1[RestsharpApiClient.model.ObjectWithRelationships]
This is my projects file
TargetFramework=netstandard2.0
<LangVersion>8.0</LangVersion>
"Newtonsoft.Json" Version="12.0.3"
"RestSharp" Version="106.11.4"
ANY HELP WOULD BE VERY MUCH APPRECIATED
>` not `List`
– Selvin Jul 27 '20 at 14:38> response = await _restClient.ExecuteAsync
– Kristoffer Rakstad Solberg Jul 27 '20 at 15:02>(request);`. It tries to execute and exites with code 0 at this line. So to be a bit more specific, the Console.Writeline(obj) is actually never reached.