简体   繁体   中英

Call a paginated service asynchronously c#

The following service is being called from my method

https://myservice?index=0&rows=500

index: Offset into a query's result set. Service will return results from this offset. Default is 0 ie starting from the first result.

rows: Number of results to be returned per request. Default is 10 and maximum number can be 500

I need to keep paginating until the start parameter has reached the totalRows count in the response JSON. Suppose if the total no.of records is 10,000 i need to call the service 20 times

Is it possible to modify the code in such a way that the subsequent service calls after first one is made asynchronous to improve the performance

public List<FinalOutputDetails> GetFinalOutput()
{
        List<FinalOutputDetails> FinalOutput = new List<FinalOutputDetails>();
        int totalRows = 500;
        int index = 0;
        int end = 500;
        while (index <= totalRows)
        {
            string result =CallRemoteService();  // call service https://myservice?index=0&amp;rows=500

            var patrialResultDetails = JsonConvert.DeserializeObject<ServiceResultDetails>(result);

            totalRows = patrialResultDetails.totalRows;

            FinalOutput.AddRange(patrialResultDetails);

            start += 500;
            end += 500;
        }
        return FinalOutput;
  }

Yes Possible, Easy make CallRemoteService() function as

async Task<string>CallRemoteService()

it means this method can be call many times before return result for first or other calls.

And call it asynchronous

Task.Run(async () => string result =await CallRemoteService() ... and do other job to add result to main total string builder here..);

with this line your application wont wait for result..

But I have to inform you. You will lose your order. If you need correct order, It means you have to wait first result and than call second. But it means your question wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM