Note that there are some explanatory texts on larger screens.

plurals
  1. POHttpContext.Current.Items after an Async operation
    primarykey
    data
    text
    <p>Consider the following ASP.NET Web API Delegating Handler: </p> <pre><code>public class MyHandler : DelegatingHandler { protected async override Task&lt;HttpResponseMessage&gt; SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) { var guid = Guid.NewGuid(); HttpContext.Current.Items["foo"] = guid; // An Async operation var result = await base.SendAsync(request, cancellationToken); //All code from this point is not gauranteed to run on the same thread that started the handler var restoredGuid = (Guid)HttpContext.Current.Items["foo"]; //Is this gauranteed to be true var areTheSame = guid == restoredGuid; return result; } } </code></pre> <p>The above example is in a delegating handler, the same problem I am trying to fix applies in Controllers, Business Objects, etc.</p> <p>I am ultimately trying to provide some simple in-memory shared state between various objects per HTTP Request</p> <p>As I understand it during Async operations the ASP.NET thread originally running the operation is returned to the thread pool and a different thread may be used to finish the request after the Async operation has completed.</p> <p>Does this affect the <code>HttpContext.Current.Items</code> collection? Is an item that was in the <code>Items</code> collection guaranteed to be there when the Request resumes?</p> <ol> <li><p>I'm aware that using <code>HttpContext.Current</code> is often frowned upon by the wider community these days for reasons I completely agree with... I'm just helping someone out of a jam.</p></li> <li><p>Storing this data in the <code>Request.Items</code> collection is not suitable to solve this problem as my colleague requires a static due to some poor design decisions.</p></li> </ol> <p>Many Thanks</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.
 

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