Note that there are some explanatory texts on larger screens.

plurals
  1. POWCF AsyncPattern + Task<T>.Factory.FromAsync = ContextSwitchDeadlock
    primarykey
    data
    text
    <p>I'm trying to simplify a call to a WCF async operation by wrapping the call into a Task on the client side with Task.Factory.FromAsync. But when I Start() the wrapped Task, the call to Start() blocks the client until the debugger reports a ContextSwitchDeadlock. The server side operation gets invoked correctly, though. What am I missing?</p> <p>WCF contract:</p> <pre><code>[ServiceContract(Namespace = "urn:test/test")] public interface ITestContract { [OperationContract(AsyncPattern=true)] IAsyncResult BeginGetResult(AsyncCallback callback, object state); int EndGetResult(IAsyncResult result); } </code></pre> <p>Client code:</p> <pre><code>var task = Task&lt;int&gt;.Factory.FromAsync( (callback, state) =&gt; service.BeginGetResult(callback, state), (result) =&gt; service.EndGetResult(result) ); task.Start(); // blocks until ContextSwitchDeadlock gets reported </code></pre> <p>EDIT: Just for the sake of completeness: No, I can't use the .NET 4.5 async / await pattern because .NET 4.0 (i.e. Windows XP and Server 2003 support) is a hard requirement for my project.</p> <p>EDIT2: </p> <p>The call to <code>Start()</code> should not be necessary anyway. The real problem is that <code>BeginGetResult</code> does not get invoked on the server side until I call <code>Start()</code>, <code>Wait()</code> or <code>Result</code> - which amount to the same thing as a synchronous call.</p> <p>As I found out, this has nothing to do with Tasks at all. If I do the following, I get the same results:</p> <pre><code>var asyncResult = service.BeginGetResult(null, null); // BeginGetResult NOT invoked on server side! asyncResult.AsyncWaitHandle.WaitOne(); // Now BeginGetResult actually gets invoked </code></pre> <p>EDIT3:</p> <p>Turns out that it was all just a misunderstanding on my part. I expected that calling <code>BeginGetResult</code> on the client side would <em>immediately</em> call the corresponding <code>BeginGetResult</code> on the server side, i.e. that <code>BeginGetResult</code> on the client side would block until the server actually gets a request, but that does not seem to be the case. Apparently the request is sent in a background thread, so there may be a delay between the client and server side call to <code>BeginGetResult</code>. I suppose this behavior is to be expected, so everthing is fine :-).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload