Note that there are some explanatory texts on larger screens.

plurals
  1. POHttpContext is null after await Task.Factory.FromAsync(BeginXxx, EndXxx)
    primarykey
    data
    text
    <p>I have an Asp.Net application that calls some WCF service using a proxy generated by svcutil. The generated proxy has async methods in the APM pattern (BeginXxx, EndXxx).</p> <p>I am using the async targeting pack in order to leverage the new <code>async/await</code> pattern, and my target platform is .net 4.0 (otherwise I wouldn't have to use the async targeting pack).</p> <p>I am using <code>Task&lt;T&gt;.Factory.FromAsync</code> to convert the APM pattern into a Task that I can await.</p> <p>my code looks a little something like that (assuming the service method takes no parameters and returns an int:</p> <pre><code>int result = await Task&lt;int&gt;.Factory.FromAsync(proxy.BeginXxx, proxy.EndXxx, state: null); HttpContext.Current.Items["reuslt"] = result; </code></pre> <p>The second line throws a <code>NullReferenceException</code>.</p> <p>I am assuming that the reason is that the callback (which is everything that's running after the await call) does not have the original synchronization context.</p> <p>other methods (like Factory.StartNew) have <a href="http://msdn.microsoft.com/en-us/library/dd781658%28v=vs.100%29.aspx" rel="nofollow">overloads</a> that receive a <code>TaskScheduler</code> that can be used to preserve the context.</p> <p>The FromAsync Method also has <a href="http://msdn.microsoft.com/en-us/library/dd321413.aspx" rel="nofollow">overloads</a> that receive a <code>TaskScheduler</code>, but none that have the begin method as an argument.</p> <p>Assuming I do not want to start passing the Http context by hand as a method argument to all methods that get called after this line, how do I use the overload that gets a task scheduler or otherwise get .Net to respect my synchronization context?</p>
    singulars
    1. This table or related slice is empty.
    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