title: Web API - Set each thread with the HttpRequestMessage id?
body: <p>I have a web api coded in c#.</p>
  
  <p>The web api uses functionality which is shared with other in-house components.
  it depends on single threaded flows and uses thread local storage to store objects, and session information.
  Please don't say if it's good or bad, that's what I have to deal with.</p>
  
  <p>In the web api I've implemented a custom message handler (DelagatingHandler) with SendAsync</p>
  
  <pre><code>protected async override System.Threading.Tasks.Task&lt;HttpResponseMessage&gt; SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
  </code></pre>
  
  <p>which is based on TPL and sometimes switches threads, and when this happens, my thread based functionality gets messed up, since I'm losing the thread context and all data assigned to it.</p>
  
  <p>My idea is to uniquely identify the HttpRequestMessage,
  I think using the correlation id should be sufficient for it</p>
  
  <pre><code>var requestId = request.GetCorrelationId();
  </code></pre>
  
  <p>But I want to store the correlation Id of the HttpRequestMessage per each thread that is allocated in the Task.</p>
  
  <p>So my question is basically if I can identify a thread that's being allocated under a specific Task and allocate the id to it?</p>