Paolo ponzano - 141 - Feb 18, 2014

I’m invoking asynchronously a service using client.PostAsync (eg. :
  var task = client.PostAsync<…>
  task.Wait()
          
task.Wait() waits undefinitively (the same happens with Task.WaitAll(…))

I’m using this since I have to call more services in parallel… it seems that the task is not marked as completed

Wayne Brantley:

I have experienced the same thing.   What context are you calling these from?  MVC controller?  Thick Client? etc?

paolo ponzano:

Wpf client
__________

Wayne Brantley:

Humm…try this crazy suggestion.  When the application first starts - before you make any calls to anything, like in the applicatino startup, do this code:
var xx=new JsonServiceClient();

And nothing else…that is what I am having to do…I know it is crazy…but we have been trying to figure this out for a couple of weeks and yours may be different than mine…but…

paolo ponzano:

Adding this works?
__________

Wayne Brantley:

For me, my MVC app does the SAME THING you are saying.   The non-async calls work.  If I change to Async, they never return.   If I create a new JsonServiceClient() in the Application_Start (and do nothing with it), they all return and work fine.   I know it is weird, believe me…

Hmmm, I just saw this, it sounds like it’s related:

"Using Task.Wait() can lead to deadlocks in ASP.NET due to the special SynchronizationContext we use. Channel 9 has a video where Brad, Damian, and I speak about this in more detail."

http://blogs.msdn.com/b/webdev/archive/2014/02/18/introducing-asp-net-project-helios.aspx

Wayne Brantley:

+Demis Bellot Agreed, for me it is related for this post he is using WPF.  I have not watched the video yet though.

paolo ponzano:

Demis I’ll prepare you a sample as sono ad I ve time… Hope you can find a Fix btw

paolo ponzano:

I’m under WPF for the client but ServiceStack is hosted on  asp.net …btw I’ve used async programming with asp.net in past without any troubles

paolo ponzano:

Demis I’ve prepared a sample project but I’m not able to upload it on github… can you please tell me where I should upload it to?
Thanks

Note: you shouldn’t block on async code, see:
http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html

Use the sync API’s if you want to perform sync/blocking operations.

paolo ponzano:

I’ll take a look on this… btw I’ve used in past the Wait.All in asp.net without any problem…can you please tell me where I can upload my demo project? If I’m in non blocking approach how should I start 4-5 load in parallel then wait?
Thanks

paolo ponzano:

Here’s the link to the demo project https://drive.google.com/file/d/0B6nPFuIuKHG1MW9LMEhXbHpRTzg/edit?usp=sharing

Paolo, the recommendation from the ASP.NET team is to not to block on async. If this is what you’re doing you’re most likely running into the dead lock problem they’re warning you about.

If you’re making the async calls in from WPF app, you can get the callback to happen on the UI thread with:

var client = new JsonServiceClient(baseUrl) {
    HandleCallbackOnUiThread = true,
};

Wayne Brantley:

+Demis Bellot are you looking at fixing that need for HandleCallbackOnUiThread?

Yeah I’ve added a CaptureSynchronizationContext option to capture the synchronization context when the call was made and executing the response on the same context in this commit:
https://github.com/ServiceStack/ServiceStack/commit/d9330daf93dd4738e405d8c75a905233fe71941e

It’s similar to the UiThread SynContext except it captures it at the callsite instead of where the client was first initialized.

Wayne Brantley:

That is great…I have MANY hours in this issue as you know!  Glad to hear it is fixed!  

+Wayne Brantley This is now on MyGet: https://github.com/ServiceStack/ServiceStack/wiki/MyGet

Can you try the CaptureSynchronizationContext option out when you have time and let me know if it also solves the issues. I’ll leave it as a non-default option for a while until it’s had some real-world testing.

paolo ponzano:

Hello Demis,
I’ve tried your modification on the demo project I’ve uploaded to my google drive but it still hangs…