我正在連接到一個REST API並調用一些端點來獲取不同的對象。我爲每一個類型RestService<T>
我想下載:並行運行一系列方法
RestService<Agent> agentService = new RestService<Agent>(auth, new AgentApi());
RestService<Ticket> ticketService = new RestService<Ticket>(auth, new TicketApi());
RestService<Company> companyService = new RestService<Company>(auth, new CompanyApi());
RestService<Contact> contactService = new RestService<Contact>(auth, new ContactApi());
對於每個RestService<T>
然後我打電話GetAll()
調用REST API,並得到結果:
RestResult<Agent> agentResults = agentService.GetAll();
RestResult<Company> companyResults = companyService.GetAll();
RestResult<Contact> contactResults = contactService.GetAll();
RestResult<Ticket> ticketResults = ticketService.GetAll();
幕後GetAll()
背後,使得一些的HttpWebRequest
resquests。
所以我在想的是以某種方式並行調用4 GetAll()
調用,理論上我可以向REST API發出多個請求,而不是一個接一個地發出請求。
我有一個想法是:
RestResult<Agent> agentResults;
RestResult<Company> companyResults;
RestResult<Contact> contactResults;
RestResult<Ticket> ticketResults;
Parallel.Invoke(
() => agentResults = agentService.GetAll(),
() => companyResults = companyService.GetAll(),
() => contactResults = contactService.GetAll(),
() => ticketResults = ticketService.GetAll()
);
但它看起來像變量從未初始化。
有關如何解決這個問題的任何建議?
什麼是「它看起來像」是什麼意思?他們仍然是'空'嗎?如果是這樣,你確定'GetAll()'方法返回了其他東西嗎? –
對不起,我的意思是我得到一個編譯錯誤,說我以後嘗試並使用它們時變量未分配。 – moose56