简体   繁体   English

异步调用分页服务 c#

[英]Call a paginated service asynchronously c#

The following service is being called from my method正在从我的方法调用以下服务

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

index: Offset into a query's result set. index:查询结果集中的偏移量。 Service will return results from this offset.服务将从这个偏移量返回结果。 Default is 0 ie starting from the first result.默认值为 0,即从第一个结果开始。

rows: Number of results to be returned per request. rows:每个请求要返回的结果数。 Default is 10 and maximum number can be 500默认为 10,最大数量为 500

I need to keep paginating until the start parameter has reached the totalRows count in the response JSON.我需要继续分页,直到开始参数达到响应 JSON 中的 totalRows 计数。 Suppose if the total no.of records is 10,000 i need to call the service 20 times假设如果记录总数为 10,000,我需要调用该服务 20 次

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是的,可以,轻松将 CallRemoteService() function 设为

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.但这意味着你的问题是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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