Passing HttpContext to Task.Run thread
<p>I'm trying to pass <code>HttpContext.Current</code> to the thread created via <code>Task.Run</code>. As I have a Transaction ID in in <code>HttpContext.Items</code>, which I'll need in the thread.</p>

<pre><code>public static Task&lt;TResult&gt; RunWithContext&lt;TResult&gt;(Func&lt;TResult&gt; function)
{
    var context = HttpContext.Current;                        
    return Task.Run(()=&gt; { RequestHelper.SaveHttpContextInThread(context); return function(); } );
}

public static void SaveHttpContextInThread(HttpContext context)
{
    Thread.SetData(Thread.GetNamedDataSlot(RequestHelper.THREAD_SLOT_NAME), context);
}


var response = TaskHelper.RunWithContext(() =&gt; _service.UpdateJob(jobjsonstring));
var jobData = JObject.Parse(response.Result); //Compiler error here
</code></pre>

<p>But a compiler error </p>

<blockquote>
  <p>'Argument 1: cannot convert from 'System.Threading.Tasks.Task'
  to 'string'</p>
</blockquote>

<p>occurs in the last line.</p>

<p>Seems to be an issue with the way I'm returning from the anonymous method. </p>

<hr>

<p>Update</p>

<p>It seems _service.UpdateJob was returning Task but I was expecting it to return Job object, Apologies.</p>

<p>Actual Problem: In a WebAPI we want to run some task in a new thread. There is a value TransactionID in HttpContext.Items. We want the newly created thread to be able to read TransactionID. The above approach was to pass the entire HttpContext to the new thread.</p>