This is the way I have gotten this to work so all my dropdowns have the correct values. It FEELS like I'm doing something wrong with it, but it works and I'm on a deadline so it will have to do for now. Maybe later I'll be able to get a better solution. Maybe someone on StackOverflow will provide a better solution - we'll see!
/// Do Stuff in Async method
using (HttpClient hc = new HttpClient())
{
hc.BaseAddress = CatalogUri;
hc.DefaultRequestHeaders.Accept.Add(HttpJsonHeader);
if (Session["StandardSearchOptions"] == null || rebindStandardOptions)
{
// request for standard options
HttpResponseMessage stdResponse = hc.PostAsJsonAsync(CatalogSearchOptionPath,
searchmeta).Result;
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);
}
Session["StandardSearchOptions"] = stdOptions;
}
else // only rebinding the custom options
{
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));
}
}
/// Do other stuff in Async Method
Async method for doing the individual custom searches:
private async Task<KeyValuePair<string, List<string>>> GetCustomOptionList(string key,
HttpClient client, SearchMetadata sm)
{
sm.OptionFieldName = key;
var response = await HttpClientExtensions
.PostAsJsonAsync(client, CatalogSpecificOptionPath, sm)
.Result.Content.ReadAsStringAsync();
return new KeyValuePair<string, List<string>>(key,
Newtonsoft.Json.JsonConvert
.DeserializeObject<List<string>>(response));
}// end task