locked
please help me simplify my WebAPI calls RRS feed

  • Question

  • Hello,

    Please have a look at the attached screen shot. It's a controller method I wrote to call a WebAPI to get a json string. It's pretty convoluted. I'm wondering if there's an easier/cleaner way to do this.

    First of all, please note that the WebAPI always requires at least 2 calls: one to get a security token, and another to send the actual request (with the security token) to do what you want.

    Even though I'm expecting at least two calls, there are still a few things I suspect could be simplified. In the screen shot, I marked out 3 things that I'm guessing have an easier way of being done:

    1) Why is it that for each call to the API, I have to do another asynchronous function call to ReadAsStringAsync() to extract the results? Isn't there a way of extracting the results directly?

    2) The inner most call to extract the results of the call to get the json string is underlined in green. When I hover my mouse over it, it says: "This async method lacks await operators and will run synchronously..." The problem is, I cannot remove the 'async' keyword from jsonStringResponse2 because then it underlines in red and tells me "cannot assign void to an implicitly-typed variable". So I need to keep the async in there and tolerate the green underline. Green underline isn't bad, but it is annoying. What's wrong with calling a function asynchronously without having an await operator in it?

    3) As you can see, I have to wait for each asynchronous call to finished before returning (hence the call to Wait() on each of the tasks before moving on). It would be nice if I could make all my API calls synchronously, but I don't seem to be able to do that.

    Thanks for any forthcoming help.

    • Moved by CoolDadTx Wednesday, April 12, 2017 2:03 PM ASP.NET related
    Tuesday, April 11, 2017 6:32 PM

All replies