我有一个 .net 站点,我试图在其中获取多个 Web 服务调用的结果,以并行方式在多个下拉列表元素之间共享。我的问题是所有下拉列表最终都具有相同的值,或者有些具有相同的值而一个具有不同的值(可能不是正确的值)。我该如何解决这个问题以并行处理这些事情?
代码更新:
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<string> keynames = {"Key3", "Key2","Key1"};
    ConcurrentDictionary<string, List<string>> customOptions = new ConcurrentDictionary<string, List<string>>();
    IEnumerable<Task<KeyValuePair<string, List<string>>>> tasks = from key in keynames select GetCustomOptionList(key, hc, searchmeta);
    customOptions = new ConcurrentDictionary<string, List<string>>(await Task.WhenAll(tasks));
    if (stdResponse.IsSuccessStatusCode)
    {
        string g = stdResponse.Content.ReadAsStringAsync().Result;
        stdOptions = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SearchOption>>(g);
        //options = response.Content.ReadAsAsync<SearchOption[]>().Result.ToList();
    }
}
执行请求的异步方法:
private async Task<KeyValuePair<string, List<string>>> 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<string, List<string>>(key, Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(result));
}// end task