parallelizing synchronous tasks while retaining the httpcontextcurrent in aspnet

Unfortunately, this library for service communication is made to be synchronous, and we want to parallelize its use.

throws null exception when HttpContext isn’t set:

The obvious answer (HttpContext.Current = parentContext) can’t work because there’s some async code underneath (for whatever reasons), and that would cause it to sometimes not return to the same thread, and basically abandon the Context, again resulting in null

There’s an important part of your question in the example code comment. 🙂

Normally, HttpContext shouldn’t be shared across threads. It’s just not threadsafe at all. But you can set HttpContext.Current (for some reason), so you can choose to live dangerously.

The more insidious problem here is that the library has a synchronous API and is doing sync-over-async – but somehow without deadlocking (?). At this point, I must be honest and say the best approach is to fix the library: make the vendor fix it, or submit a PR, or just rewrite it if you have to.

However, there is a tiny chance that you can get this kinda sorta working by adding Even More Dangerous code.

So, here’s the information you need to know:

  • ASP.NET (pre-Core) uses an AspNetSynchronizationContext. This context:
    • Ensures that only one thread runs in this context at a time.
    • Sets HttpContext.Current for any thread that is running in the context.

Now, you could capture the SynchronizationContext.Current and install it on the thread pool threads, but in addition to being Very Dangerous, it would not achieve your actual goal (parallelization), since the AspNetSynchronizationContext only allows one thread in at a time. The first portion of the 3rd-party code would be able to run in parallel, but anything queued to the AspNetSynchronizationContext would run one thread at a time.

So, the only way I can think of making this work is to use your own custom SynchronizationContext that resumes on the same thread, and set HttpContext.Current on that thread. I have an AsyncContext class that can be used for this:

[HttpPost]
public async Task<IHttpActionResult> DoIt(IEnumerable<int> inputs)
{
  var context = HttpContext.Current;
  var tasks = inputs.Select(i =>
      Task.Run(() =>
          AsyncContext.Run(() =>
          {
            HttpContext.Current = context;
            var results = Some3rdPartyTool.CallEndpointSynchronously(MyRestEndpointConfig[i]);
            return results;
          })));
  var outcome = await Task.WhenAll(tasks);
}

So for each input, a thread is grabbed from the thread pool (Task.Run), a custom single-threaded synchronization context is installed (AsyncContext.Run), HttpContext.Current is set, and then the code in question is run. This may or may not work; it depends on how exactly Some3rdPartyTool uses its SynchronizationContext and HttpContext.

Note that there are several bad practices in this solution:

  • Using Task.Run on ASP.NET.
  • Accessing the same HttpContext instance simultaneously from multiple threads.
  • Using AsyncContext.Run on ASP.NET.
  • Blocking on asynchronous code (done by AsyncContext.Run and also presumably Some3rdPartyTool.

In conclusion, I again recommend updating/rewriting/replacing Some3rdPartyTool. But this pile of hacks might work.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top