Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Getting result of a task
Message
 
 
À
18/11/2016 06:28:15
Information générale
Forum:
ASP.NET
Catégorie:
Common Language Runtime
Versions des environnements
Environment:
C# 5.0
OS:
Windows 10
Database:
MS SQL Server
Divers
Thread ID:
01643645
Message ID:
01643792
Vues:
53
UPDATE 2. I found http://stackoverflow.com/questions/10547895/how-can-i-tell-when-httpclient-has-timed-out but not yet sure how to incorporate into my code.

It is very late here, so I'll try to work on that problem tomorrow.

UPDATE. Here is my current solution - but I now need to somehow implement number of tries and correctly work with the timeout errors:
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.

Also, how should I simulate the timeout response?

Thanks again.

>>Hi everybody,
>>
>>I've defined the following method:
>>
>>
>>static async Task<HttpResponseMessage> GetResponse(TicketUsageRequests ticket, string apiUrl)
>>        {
>>            HttpResponseMessage response = await client.PostAsJsonAsync(apiUrl, ticket);
>>            //response.EnsureSuccessStatusCode();
>>            
>>            return response;
>>        }
>>
>>My question is - how can I get its result in the main method code and synchronously?
>>
>>My current method is:
>>
>>
>>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
>>
>>   ...
>>
>>
>>I found this
>>
>>http://stackoverflow.com/questions/5095183/how-would-i-run-an-async-taskt-method-synchronously
>>
>>and trying to figure out how to apply pixel's solution.
>>
>>Thanks in advance.
>
>Not 100% sure but just try:
HttpResponseMessage response = client.PostAsJsonAsync(apiUrl, ticket).Result;
I'm now back to the next hurdle I don't know how to solve :(

The code from https://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client shows how to get product back (in UpdateProduct method).

In my case I'm trying to use synchronous methods (because my main function is going to pass a few parameters by reference).

So, I got this:
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.

Thanks in advance.
}
If it's not broken, fix it until it is.


My Blog
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform