async Task<TicketUsageResponse> GetResponse(CityPassJSON json) { TicketUsageResponse ticketUsageResponse = null; using (HttpResponseMessage response = await client.PostAsJsonAsync("/integrationapi//Attraction/Tickets/Validate", json)) { if (response.IsSuccessStatusCode) { using (HttpContent content = response.Content) { ticketUsageResponse = await content.ReadAsAsync<TicketUsageResponse>(); } } else { Int32 statusCode = (Int32) response.StatusCode; throw new HttpException(statusCode, response.ReasonPhrase); } } return ticketUsageResponse; }and here is the main function which is going to be called from VFP somehow:
Int32 statusCode = 1; try { SetRequestHeaders(tcHost, tcUserName, tcPassword, tnTimeout); var json = new CityPassJSON(); var ticket = new TicketUsageRequest(tcBarcode, tcUsageDate, tcUserName); json.TicketUsageRequests.Add(ticket); Task<TicketUsageResponse> task = GetResponse(json); task.Wait(); TicketUsageResponse result = task.Result; tcMessage = result.ReturnData[0].Message; tcReturnValueText = result.ReturnValueText; tnReturnValue = result.ReturnValue; tcProductCode = result.ReturnData[0].ExternalProductCode; } catch (Exception e) { tcReturnValueText = e.Message; statusCode = 0; } return statusCode;So, in the body of that function I need to somehow implement my tries. This is part which is not clear to me as that timeout is currently going to go to the exception block, I think.
>>static async Task<HttpResponseMessage> GetResponse(TicketUsageRequests ticket, string apiUrl) >> { >> HttpResponseMessage response = await client.PostAsJsonAsync(apiUrl, ticket); >> //response.EnsureSuccessStatusCode(); >> >> return response; >> }>>
>>try >> { >> SetRequestHeaders(tcHost, tcUserName, tcPassword, tnTimeout, tnRetries); >> >> TicketUsageRequests ticket = new TicketUsageRequests(); >> ticket.TicketBarcode = tcBarcode; >> ticket.TicketUsageDate = tcUsageDate; >> ticket.UserID = tcUserName; >> >> ... here I want to get the result of the response and parse it >> >> ... >>>>
HttpResponseMessage response = client.PostAsJsonAsync(apiUrl, ticket).Result;I'm now back to the next hurdle I don't know how to solve :(
if (response.IsSuccessStatusCode) { TicketUsageResponse ticketUsageResponse = response.Content.ReadAsAsync<TicketUsageResponse>(); // This line does not compile }In other words, I need to get the object back. If I add await when I need to change the whole method.