[Solved] Web service call takes too long


Lose the getBookObject function, you’re just complicating things by starting a new Task there.

You can simply await the result from getBooksByAuthor if you make that function async.

public async Task<Book> getBook(int id)
{
    string urlAction = String.Format("api/book/{0}", id);
    return await GetWSObject<Book>(urlAction);
}

public async Task<string> getBooksByAuthor (int authorId) 
{
    string result = "";

    var books = from a in db.Authors
                where a.id == authorId
                select new
                {
                    id = a.id
                };

    foreach (var book in books.ToList())
    {
        var bookdata = await this.getBook(book.id);
        result += bookdata.name + ", ";   
    }

    return result; 
}

If you want to to make concurrent http requests, you can create a number of Tasks and use Task.WhenAll, e.g.

https://stackoverflow.com/a/30668277/1538039

4

solved Web service call takes too long