1

I have an async Web API called MyCustomAPI to read some data from an API hosted in Azure, process it and save it in a SQL server. I have made the MyCustomAPI Web API, Azure API and the call to the SQL server as async calls. I have to call the ReadAndSaveDataCalleeAsync that calls the Web API method MyCustomAPI.ReadAndSaveDataAsync() in a command line app.

The problem that I have is I can't make Async calls in the Main method. I have to call with a Wait() method. This will make the Main method to wait, so make it synchronous.

static void Main()
{
  ReadAndSaveDataCalleeAsync().Wait(); // Calls MyCustomAPI.ReadAndSaveDataAsync
}

I think this defeat the purpose of making (1) MyCustomerAPI (2) Azure API (3) Database calls async. My understanding is that I have to make all the calls async to get the benefit of the Operating System handle the resources and threads for all the methods very efficiently.

How can I make the Command line app async to get the benefit of all other services async feature?

wonderful world
  • 10,969
  • 20
  • 97
  • 194
  • 1
    The main benefit of async is that your web server doesn't have a blocked thread. You'll obviously have to wait for the result in your console app if you want to do anything with it. – juharr Mar 26 '17 at 01:24
  • 1
    What else are you doing in the program? – EJoshuaS - Stand with Ukraine Mar 26 '17 at 01:25
  • 1
    This may help you: http://stackoverflow.com/questions/40249169/async-await-and-threading – EJoshuaS - Stand with Ukraine Mar 26 '17 at 01:28
  • 1
    A command line process ends once it exits the Main method--closing all associated processes/threads. So if you *don't* make it wait, it will shut down that async process before it has completed. Which is a long way of saying "this is by design and for very good reason and probably what you want, anyway". – Jacob Proffitt Mar 26 '17 at 01:32
  • Since it is very unclear what benefits you are looking for at this point post sounds like perfect duplicate of standard "async and Command line" posts. If those post did not answer your question make sure to clarify what benefits you expect to gain in command line application from using async methods. – Alexei Levenkov Mar 26 '17 at 02:30
  • I updated with the benefit in the question. *Juharr* and *Jacob Proffitt*'s comments answer my question. Though I use ```Wait()``` in the ```Main``` method, I can still get the benefit of OS scaling the calls in MyCustomAPI, Azure API and DB interaction. – wonderful world Mar 26 '17 at 04:23

1 Answers1

1

The method from where you call your API should have an async modifier and the return type should be Task or Task<T>, as it is shown in this example from MSDN:

// Three things to note in the signature:  
//  - The method has an async modifier.   
//  - The return type is Task or Task<T>. (See "Return Types" section.)  
//    Here, it is Task<int> because the return statement returns an integer.  
//  - The method name ends in "Async."  
async Task<int> AccessTheWebAsync()  
{   
    // You need to add a reference to System.Net.Http to declare client.  
    HttpClient client = new HttpClient();  

    // GetStringAsync returns a Task<string>. That means that when you await the  
    // task you'll get a string (urlContents).  
    Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");  

    // You can do work here that doesn't rely on the string from GetStringAsync.  
    DoIndependentWork();  

    // The await operator suspends AccessTheWebAsync.  
    //  - AccessTheWebAsync can't continue until getStringTask is complete.  
    //  - Meanwhile, control returns to the caller of AccessTheWebAsync.  
    //  - Control resumes here when getStringTask is complete.   
    //  - The await operator then retrieves the string result from getStringTask.  
    string urlContents = await getStringTask;  

    // The return statement specifies an integer result.  
    // Any methods that are awaiting AccessTheWebAsync retrieve the length value.  
    return urlContents.Length;  
}  

And here is the link: https://msdn.microsoft.com/en-us/library/mt674882.aspx

mako
  • 78
  • 1
  • 6