Request the same web service asynchronously several times

I am creating a razor that consumes a web service (not owned by our organization). The method of calling the service is as follows:

  • Call the first method that returns multiple commands.
  • For each of these commands, call the second service asynchronously to return a record.

I need to make the service method an asynchronous call for performance. The problem I am facing is that I have no way to wait until ALL answers are available before returning the records back to the view. I'm so far away:

The way to return records to razor mode:

    public List<ProactisContract> GetContractsList()
    {
        List<Guid> contractIds = GetAmendedContracts();
        GetContractDetails(contractIds);

        //Test
        System.Threading.Thread.Sleep(5000);

        return _contractList;
    }

This is the second method that goes through the prompts from the first call, making a service request for each record:

    private void GetContractDetails(List<Guid> contractIds)
    {
        foreach (var recId in contractIds)
        {
            var request = new GetContractDetailsRequest { Authentication = _authorisation, ContractGuid = recId, ContractNumber = "string", SummaryOnly = true };

            AsyncCallback asyncCallback = GetContractDetailsCallBack;

            _service.BeginGetContractDetails(request, asyncCallback, _service);
        }
    }

    private void GetContractDetailsCallBack(IAsyncResult asyncResult)
    {
        var response = _service.EndGetContractDetails(asyncResult);

        lock (_contractList)
        {
            var contract = new ProactisContract
            {
                /*Assign property values*/
            };
            _contractList.Add(contract);
        }
    }

, , < > ?

+4
1

, contractIds, , , 1,

while (counter != 0)
{
}

.

, , . , - , .

+1

All Articles