Saturday, 15 May 2010

c# - HttpClient - Send a batch of requests -


Text after "

To replicate a batch of requests, using the HttpClient class, we want to send the external API to each one of them .

  foreach (var MyRequest in requests) {try {http client http client = new HTTP client}; HttpClient.Timeout = Timespan From Milliseconds (5); HttpResponseMessage Response = Wait httpClient.PostAsJsonAsync & LT; String & gt; (String.Format ("{0} API / GetResponse", last point), myRequest; Job Result Response = Response Wait for the seminar. Redsign & Lieutenant; Jobs & gt; (); } Hold (exception before) {continue; }}  

Reference here is required to set a very small time value value, so if the response takes longer than that time, then we only "canceled the job" Obtain exceptions and continue repeating again

Now, in the above code, these two lines comment:.

  HttpResponseMessage response = wait httpclient.PostAsJsonAsync & LT; String & gt; (String.Format ("{0} API / Gatorpons", Endpoint), MyRCC); The result is awaiting response = response. the seminar. Readsink & lt; Jobs & gt; ();  

The walk becomes very fast, delete them and try again. it takes a lot of time. ?

I wonder if PostAsJsonAsync with forward / ReadAsAsync calling methods take longer than the timeout value

Based on the answers below, assume it will create a separate thread, we This is the method:

Public work & lt; Jobjject & gt; GetResponse (string endpoint, JObject request, TimeSpan timeout) {return Task.Run (async () = & gt; {try {HttpClient httpclient = new HttpClient (); httpClient.Timeout = TimeSpan.FromMilliseconds (5); HttpResponseMessage response = wait httpclient .ostAsJsonAsync & lt; string & gt; (string.Format ( "{0} API / Getresspens" Andpe য়েন্ট), request) Ktaimaut & lt; Actiteepeepis message waiting (timeout) with the message; Jobik results response = response Content.RdsICINC & Lt. Jobes & gt; (). With Timeout & lt; Jobbject & Gt; (timeout); return resultResponse;} hold (ex before) {new JObject (return) {new JProperty ("ControlledException" "Invalid response."}}}}}); }

An exception has been raised there and Jobk exception should be returned to the very fast, however, if you use the methods of HTML class, even if this exception raises, too takes time. Is there a project behind the curtains affecting the work, even if the return value was a simple exception?

If so, then another approach can be used to send the request of the API very fast, by the way?

It does not seem that you are actually running different threads for each request to do something like this. Try:

  var taskList = new list & lt; Work & lt; Jobs & gt; & Gt; (); Foreign currency (requests in my requestest request) {taskList.Add (GetResponse (endPoint, myRequest)); } Try {Tasks}. Waiting for all (Agenda. To Array ()); } Grip (Exceptional) {} Public Work & lt; Jobjject & gt; GetResponse (string endpoint, string myRequest) {return Task.Run (() = & gt; {HttpClient httpclient = new HttpClient (); HttpResponseMessage response = httpClient.PostAsJsonAsync & LT; string & gt; (String.Format ( "{0 } API / GetResponse ", the last point), myRequest, new CancellationTokenSource (TimeSpan.FromMilliseconds (5)) token) ;. JObject resultResponse = response.Content.ReadAsAsync & LT; JObject & gt; ();}); }  

No comments:

Post a Comment