I have to use the results in first async call to call the second async method but web api returns empty results before it finish calling method 2?
I have tryed using .ContinueWith but that appears in a deadlock
Could someone lead me to some kind of a solution?
example:
public class AsyncApiController : ApiController
{
[ActionName("GetClientBySSN")]
public async Task<IHttpActionResult> GetClientBySSN(string ssn)
{
return Ok(await _repository.GetClientBySSN(ssn));
}
}
public interface IResRepository
{
Task<ClientResponse> GetClientBySSN(string ssn);
}
public class ResRepository : IResRepository
{
public Task<ClientResponse> GetClientBySSN(string ssn)
{
//async method
Task task1 = _service.GetClientNumberBySSN(ssn);
int clientNumber = task1.Result.clientnumber;
if (clientNumber != null)
{
//another async method that uses ID from the first result
return _service.GetClientDetailsByClientNumber(clientNumber);
}
else
{
return null;
}
}
}
int clientNumber = (await task1).clientnumber;. Don't block a Task usingTask.Resultwhen you can await it.