Note that there are some explanatory texts on larger screens.

plurals
  1. POConcurrently fill a dictionary with HttpClient PostAsJsonAsync extension
    text
    copied!<p>I have a .net site in which I'm trying to get the results of several web service calls, to be shared among several dropdownlist elements, in a parallel fashion. My problem is all of the dropdowns end up having the same values, or some have the same values with one having different values (probably not the correct ones). How can I fix this to get these things in parallel?</p> <p><em><strong>Code Updated:</em></strong></p> <pre><code>using (HttpClient hc = new HttpClient()) { hc.BaseAddress = new Uri(CatalogUri); hc.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/jsonp")); // request for standard options HttpResponseMessage stdResponse = hc.PostAsJsonAsync(CatalogSearchOptionPath, searchmeta).Result; List&lt;string&gt; keynames = {"Key3", "Key2","Key1"}; ConcurrentDictionary&lt;string, List&lt;string&gt;&gt; customOptions = new ConcurrentDictionary&lt;string, List&lt;string&gt;&gt;(); IEnumerable&lt;Task&lt;KeyValuePair&lt;string, List&lt;string&gt;&gt;&gt;&gt; tasks = from key in keynames select GetCustomOptionList(key, hc, searchmeta); customOptions = new ConcurrentDictionary&lt;string, List&lt;string&gt;&gt;(await Task.WhenAll(tasks)); if (stdResponse.IsSuccessStatusCode) { string g = stdResponse.Content.ReadAsStringAsync().Result; stdOptions = Newtonsoft.Json.JsonConvert.DeserializeObject&lt;List&lt;SearchOption&gt;&gt;(g); //options = response.Content.ReadAsAsync&lt;SearchOption[]&gt;().Result.ToList(); } } </code></pre> <p>Async method for doing the requests:</p> <pre><code>private async Task&lt;KeyValuePair&lt;string, List&lt;string&gt;&gt;&gt; GetCustomOptionList(string key, HttpClient client, SearchMetadata sm) { sm.OptionFieldName = key; var response = await client.PostAsJsonAsync(CatalogSpecificOptionPath, sm); var result = await response.Content.ReadAsStringAsync(); return new KeyValuePair&lt;string, List&lt;string&gt;&gt;(key, Newtonsoft.Json.JsonConvert.DeserializeObject&lt;List&lt;string&gt;&gt;(result)); }// end task </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload