I'm working on converting my code to async and I'm stumped with this problem. My CallRestAPI is not returning. I can debug every line of code, even down to the return e; but it then goes to the last bracket and then hangs. for code completeness, I moved the httpclient into this method so a complete code example.
from the UI Thread, I call - [Dim result = ExecuteDSSTP(params...)].
The code does not return from CallRestAPI when serializing. It seems to run all the way through, but doesn't return.
public DataSet ExecuteDSSTP(string asStoredProcedure, params object[] aoParams)
{
return ExecuteDSSTPAsync(asStoredProcedure, aoParams).Result;
}
public async Task<DataSet> ExecuteDSSTPAsync(string asStoredProcedure, params object[] aoParams)
{
dbUtilityResponse retVal = new dbUtilityResponse();
retVal = await CallRestAPI<dbUtilityResponse>("ExecuteSTP", new DbUtilityRequest() {Params....});
return retVal.ResultDataSet;
}
private static async Task<T> CallRestAPI<T>(string method, object body)
{
var byteArrayContent = new ByteArrayContent(SerializeBson(body));
byteArrayContent.Headers.ContentType = new MediaTypeHeaderValue("application/bson");
var _httpClient = new HttpClient();
_httpClient.DefaultRequestHeaders.Accept.Clear();
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/bson"));
_httpClient.DefaultRequestHeaders.Add("Connection", "keep-alive");
var response2 = await _httpClient.PostAsync("https://myurl/" + method, byteArrayContent).ConfigureAwait(continueOnCapturedContext: false);
var ms = new MemoryStream(await response2.Content.ReadAsByteArrayAsync());
using (var reader = new BsonDataReader(ms))
{
var serializer = new JsonSerializer();
var e = serializer.Deserialize<T>(reader);
return e;
}
}