Click here to Skip to main content
15,911,646 members
Home / Discussions / C#
   

C#

 
QuestionHow to write a generic code to make async call to Rest Service Pin
ArunHanu22-Mar-17 0:31
ArunHanu22-Mar-17 0:31 
AnswerRe: How to write a generic code to make async call to Rest Service Pin
Nathan Minier22-Mar-17 1:13
professionalNathan Minier22-Mar-17 1:13 
GeneralRe: How to write a generic code to make async call to Rest Service Pin
ArunHanu22-Mar-17 1:43
ArunHanu22-Mar-17 1:43 
GeneralRe: How to write a generic code to make async call to Rest Service Pin
Pete O'Hanlon22-Mar-17 2:03
mvePete O'Hanlon22-Mar-17 2:03 
GeneralRe: How to write a generic code to make async call to Rest Service Pin
Nathan Minier22-Mar-17 2:08
professionalNathan Minier22-Mar-17 2:08 
SuggestionRe: How to write a generic code to make async call to Rest Service Pin
Richard Deeming22-Mar-17 2:23
mveRichard Deeming22-Mar-17 2:23 
GeneralRe: How to write a generic code to make async call to Rest Service Pin
ArunHanu22-Mar-17 3:06
ArunHanu22-Mar-17 3:06 
GeneralRe: How to write a generic code to make async call to Rest Service Pin
Nathan Minier22-Mar-17 3:53
professionalNathan Minier22-Mar-17 3:53 
I'm sorry Richard, that's not correct.

Using the async keyword can mean that you want to return a Task that is not yet resolved, but doesn't necessarily mean that. It can simply mean that you're coalescing an asynchronous operation into a synchronous context, which is precisely what "await" does.

Using the await keyword means that you want to use the value provided by Task.Result, not the Task itself. It blocks execution of the method (ie runs synchronously) until the awaited Task is completed. That means that while the method is flagged as async, it resolves async operations into a synchronous context. I understand that the terminology is a bit misleading and the documentation does not help. But the fact is that if you're blocking the thread, you are running synchronously.

If the method were run asynchronously, it would look more like this:
C#
public async Task<T> RefreshData<T>(string url)
{
   return Task<T>.Run(()=>
   {
      var response = await client.GetAsync (url);
      if (response.IsSuccessStatusCode) {
         var content = await response.Content.ReadAsStringAsync ();
         var items = JsonConvert.DeserializeObject<T>(content);
         if(items != null)
         {
            return items;
         }
         throw new Exception($"Service did not provide a response formatted as type: {typeof(T)}");
      }else{
         throw new Exception($"Service request failure. Code: {response.StatusCode}");
      }
   })
   .ContinueWith(ex => { Debug.WriteLine ($"ERROR {ex.Message}");}, TaskContinuationOptions.OnlyOnFaulted));
}

I just didn't want to confuse the poor guy with Task execution and moving between synchronous and asynchronous contexts when he doesn't even have generics down.
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli

GeneralRe: How to write a generic code to make async call to Rest Service Pin
Richard Deeming22-Mar-17 4:20
mveRichard Deeming22-Mar-17 4:20 
GeneralRe: How to write a generic code to make async call to Rest Service Pin
Nathan Minier22-Mar-17 4:59
professionalNathan Minier22-Mar-17 4:59 
GeneralRe: How to write a generic code to make async call to Rest Service Pin
Richard Deeming22-Mar-17 5:26
mveRichard Deeming22-Mar-17 5:26 
GeneralRe: How to write a generic code to make async call to Rest Service Pin
Nathan Minier22-Mar-17 6:06
professionalNathan Minier22-Mar-17 6:06 
GeneralRe: How to write a generic code to make async call to Rest Service Pin
Richard Deeming22-Mar-17 6:16
mveRichard Deeming22-Mar-17 6:16 
GeneralRe: How to write a generic code to make async call to Rest Service Pin
Nathan Minier23-Mar-17 1:12
professionalNathan Minier23-Mar-17 1:12 
QuestionWindows 10 Thin Border Cause a space issue compare to windows 8 Pin
Member 979968321-Mar-17 20:12
Member 979968321-Mar-17 20:12 
AnswerRe: Windows 10 Thin Border Cause a space issue compare to windows 8 Pin
Gerry Schmitz23-Mar-17 17:52
mveGerry Schmitz23-Mar-17 17:52 
QuestionGetting COM port list ? Pin
Member 245846721-Mar-17 16:44
Member 245846721-Mar-17 16:44 
AnswerRe: Getting COM port list ? Pin
Richard MacCutchan21-Mar-17 23:11
mveRichard MacCutchan21-Mar-17 23:11 
GeneralRe: Getting COM port list ? Pin
Member 245846722-Mar-17 15:28
Member 245846722-Mar-17 15:28 
AnswerRe: Getting COM port list ? Pin
Ralf Meier23-Mar-17 1:26
mveRalf Meier23-Mar-17 1:26 
AnswerRe: Getting COM port list ? Pin
Gerry Schmitz23-Mar-17 17:51
mveGerry Schmitz23-Mar-17 17:51 
QuestionDCMTK help needed Pin
Tetsuo_DarkK21-Mar-17 4:49
Tetsuo_DarkK21-Mar-17 4:49 
AnswerRe: DCMTK help needed Pin
Richard MacCutchan21-Mar-17 4:56
mveRichard MacCutchan21-Mar-17 4:56 
QuestionQuick MEF Question Pin
Kevin Marois20-Mar-17 7:04
professionalKevin Marois20-Mar-17 7:04 
AnswerRe: Quick MEF Question Pin
Richard MacCutchan20-Mar-17 23:02
mveRichard MacCutchan20-Mar-17 23:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.