我有一个函数,它返回自动完成列表中的名称列表,并从Web服务获取数据:
public async Task<SelectResponse> AutoSearchService(string filter)
{
SelectResponse ResponseData = new SelectResponse();
var Url = string.Format("{0}{1}", "http://url&", filter);
var Client = new HttpClient();
Client.BaseAddress = new Uri(Url.ToString());
Client.DefaultRequestHeaders.Accept.Clear();
Client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/xml"));
var ResponseMessage = await Client.GetAsync(Url.ToString());
if (ResponseMessage.IsSuccessStatusCode)
{
ResponseData = await ResponseMessage.Content.ReadAsAsync<SelectResponse>();
}
return ResponseData;
}
这是AutoCompleteSearch:
private async Task<List<Autocomplete>> _GetSchool(string query)
{
var response = await AutoSearchService("");
List<STVDPLMRECORDSELECT> searchFilter = response.SelectResult;
List<Autocomplete> secondarySchools = new List<Autocomplete>();
try
{
var results = (from s in searchFilter
where (s.STVDPLM_DESC).StartsWith(query)
select s).Take(10).ToList();
foreach (var r in results)
{
// create objects
Autocomplete schools = new Autocomplete();
int i = 0;
schools.Name =r.STVDPLM_DESC;
schools.Id = i++ ;
secondarySchools.Add(schools);
}
}
catch (EntityCommandExecutionException eceex)
{
if (eceex.InnerException != null)
{
throw eceex.InnerException;
}
throw;
}
catch
{
throw;
}
return secondarySchools;
}
每次输入任何字符后都需要很长时间才能获得自动完成列表,因此我需要在运行控制器后立即读取此异步Web服务列表,使其像静态列表一样。 请帮助解决这个问题。