0
HttpResponseMessage myResponse = myClient.PostAsync(theUri, theContent).Result;

i am passing uri in theUri and json data in theContent

this works fine in android and ios but it does not respond in windows phone is anything wrong in this statement

Amit Kumar Ghosh
  • 3,618
  • 1
  • 20
  • 24
Jay Patel
  • 528
  • 8
  • 26

1 Answers1

0

If you use Result means that your method will block. But it isn't recommended because it will cause a deadlock. You need to be careful when you use Result property or Wait().

When you use async method I recommend you que you declare async method and you call the function with await.

public async Task Method()
{
    HttpResponseMessage myResponse = await myClient.PostAsync(theUri, theContent);
}

If you can't declare the method as async and you need to use a async method. I recommend you that use this class for convert async method in sync method: How would I run an async Task<T> method synchronously?

Community
  • 1
  • 1
Iván Oliver
  • 126
  • 3
  • now i am using async await now it does goes into deadlock but problem is i am not able get result inside myResponse I think still request is not sent from app to service (i am not sure) – Jay Patel Sep 09 '15 at 13:40
  • I recommend you that you use the helper class of the link and you put a breakpoint in the catch, because there are times that the error is hidden. – Iván Oliver Sep 09 '15 at 14:09
  • somewhere i read use await TaskScheduler.Default.SwitchTo(); // When blocking on Result from UI thread. but i am getting error at "await TaskScheduler.Default.SwitchTo();" Error : 'System.Threading.Tasks.TaskScheduler' does not contain a definition for 'SwitchTo' and no extension method 'SwitchTo' accepting a first argument of type 'System.Threading.Tasks.TaskScheduler' could be found (are you missing a using directive or an assembly reference?) – Jay Patel Sep 10 '15 at 05:52