MarimerLLC / cslaforum

Discussion forum for CSLA .NET
https://cslanet.com
Other
31 stars 6 forks source link

async Task DataPortal - Blocking #112

Open keithdv opened 8 years ago

keithdv commented 8 years ago

I'm having blocking problems calling an 'async Task DataPortal_Fetch' using a synchronous DataPortal.Fetch<>() call that I don't understand. This is running in two-tier. When I do the application hangs at the first 'await ...' within the DataPortal_Fetch method. Am I doing somthing wrong? Like other areas (ObjectFactory) is this supported but not recommended?

Sandbox project. The two red buttons in the CslaAsyncAwait WPF application highlight the issue. https://github.com/keithdv/CslaAsyncAwait

rockfordlhotka commented 8 years ago

Yes, there are known issue with certain combinations of sync/async behavior when using LocalProxy.

This is because LocalProxy works differently depending on whether the client makes a sync or async call.

If the client makes a sync call LocalProxy makes the most lightweight call possible - just invoking the 'server-side' data portal code on the existing thread.

If the client makes an async call LocalProxy spins the 'server-side' data portal call off to a thread from the thread pool, thus better emulating things like the WcfProxy or HttpProxy. Most importantly though, if the call isn't spun off onto another thread then there's no way to get the expected semantic behavior of the client doing something like await DataPortal.FetchAsync.

Keeping the sync call on the same thread was a conscious decision I made when I added async/await support. Specifically I thought it was worth the tradeoff of being unable to do a couple scenarios in order to retain the semantic behavior LocalProxy had prior to that point, and also to maintain the lowest overhead in that scenario.

Of course the data portal proxy is pluggable, so someone could easily enough make a copy of LocalProxy and make different choices for their application.

JasonBock commented 8 years ago

But here's the problem. If you use LocalProxy with a sync DP call to invoke an async DP operation, the code hangs. I'd much rather see CSLA throw some kind of exception in this case, rather than just hang WPF.

rockfordlhotka commented 8 years ago

I suppose the 'server-side' data portal could see that

  1. The client made a sync call
  2. ExecutionLocation is on the client
  3. The concrete method about to be invoked is async Task

Then it could throw a NotSupportedException

JasonBock commented 8 years ago

That would be awesome. Do you want me to make an issue for this in the CSLA GitHub site?

rockfordlhotka commented 8 years ago

And a PR? :smiling_imp:

JasonBock commented 8 years ago

Whoa, there! I'm sure someone will submit a PR...just won't be me :)