I am connecting to a REST API and calling a number of end points to get different objects. I create a RestService<T>
for each type I want to download:
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());
For each RestService<T>
I then call GetAll()
to call the REST API and get the results:
RestResult<Agent> agentResults = agentService.GetAll();
RestResult<Company> companyResults = companyService.GetAll();
RestResult<Contact> contactResults = contactService.GetAll();
RestResult<Ticket> ticketResults = ticketService.GetAll();
Behind the scenes GetAll()
makes a number of HttpWebRequest
resquests.
So what I am thinking is to somehow call the 4 GetAll()
calls in parallel as in theory I can make multiple requests to the REST API rather than one after the other.
One idea I had was:
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()
);
But it looks like the variables are never initialized.
Any suggestions about how to approach this?