Best way to call many web services?

后端 未结 2 873
予麋鹿
予麋鹿 2021-02-07 22:08

I have 30 sub companies and every one has implemented their web service (with different technologies).

I need to implement a web service to aggregate them, for example,

2条回答
  •  心在旅途
    2021-02-07 22:28

    You can create an async version of the GetPoint:

    public abstract class BaseClass
    { // all same attributes and methods
        public abstract long GetPoint(int nationalCode);
    
        public async Task GetPointAsync(int nationalCode)
        {
             return await GetPoint(nationalCode);
        }
    }
    

    Then, collect the tasks for each client call. After that, execute all tasks using Task.WhenAll. This will execute them all in parallell. Also, as pointed out by Kirill, you can await the results of each task:

    var tasks = Clients.Select(x => x.GetPointAsync(nationalCode));
    long[] results = await Task.WhenAll(tasks);
    

    If you do not want to make the aggregating method async, you can collect the results by calling .Result instead of awaiting, like so:

    long[] results = Task.WhenAll(tasks).Result;
    

提交回复
热议问题