Preserve HttpContext when going async with WebAPI (Medium Trust)

I am building a set of ASP.Net hosted WebAPI services that must use an old library which depends heavily on HttpContext.Current. I am having trouble ensuring that context is preserved in all the methods that participate in an async call. I have tried several variations with await/Task.Wait and TaskScheduler.FromCurrentSynchronizationContext() on the below code.

    [HttpGet]
    public Task<IEnumerable<string>> ContinueWith()
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");  //or another culture that is not the default on your machine
        Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

        var output = new List<string> { TestOutput("Action start") };

        var task = Task.Factory.StartNew(() =>
            {
                Thread.Sleep(1000);
                return TestOutput("In Task");
            }).ContinueWith(slowString =>
            {
                output.Add(slowString.Result);

                output.Add(TestOutput("Action end"));
                return output as IEnumerable<string>;
            });

        output.Add(TestOutput("Action Mid"));

        return task;
    }

    private string TestOutput(string label)
    {
        var s = label + " ThreadID: " + Thread.CurrentThread.ManagedThreadId.ToString(CultureInfo.InvariantCulture);
        s += " " + Thread.CurrentThread.CurrentCulture.EnglishName;
        s += HttpContext.Current == null ? " No Context" : " Has Context";
        Debug.WriteLine(s);
        return s;
    }

I would like to be able to ensure that the CurrentCulture is fr-FR, and that HttpContext.Current is not null at each point where TestOutput is called. I have not succeeded in doing that for the “In Task” call with anything I have tried. Also in some of my test thread id never varies suggesting that I have effectively removed the asynchronicity of the method. How can I ensure that the culture and HttpContext.Current are preserved at each call to TestOutput, and that the code is free to run on different threads?

Capturing HttpContext.Current in a closure and then simply setting it again will not work for me as I need to support Medium Trust which will throw a security exception when calling the HttpContext.Current setter.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

A little noticed fact, HttpContext.Current is writable.

var context = HttpContext.Current;
var task = Task.Factory.StartNew(() => {
    HttpContext.Current = context;
    // You may want to set CultureInformation here too.

    return TestOutput("In Task");
});

Method 2

Context is preserved whenever you await tasks.

What you’re seeing is that there’s no context for thread pool tasks (Task.Run, TaskFactory.StartNew, or for that matter BackgroundWorker or Thread or Delegate.BeginInvoke). This is normal and expected.

So, don’t use a thread pool task. Your example code seems to want to do parallel processing with multiple threads having the HttpContext, which simply isn’t possible.

You can do concurrent async methods if you want, but this requires that your Thread.Sleep can actually be an async method instead of a CPU-based method:

[HttpGet]
public async Task<IEnumerable<string>> Test()
{
  Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
  Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

  var output = new List<string> { TestOutput("Action start") };

  var task = SlowStringAsync();
  output.Add(TestOutput("Action Mid"));
  output.Add(await task);
  output.Add(TestOutput("Action end"));
  return output;
}

public async Task<string> SlowStringAsync()
{
  await Task.Delay(1000);
  return TestOutput("In Task");
}

If your old library is out of your control and you can’t make it async, then you’ll have to call it synchronously. It’s acceptable to call a synchronous method from an async method in situations like this:
[HttpGet]
public async Task<IEnumerable<string>> Test()
{
  Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
  Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

  var output = new List<string> { TestOutput("Action start") };

  output.Add(TestOutput("Action Mid"));
  Thread.Sleep(1000);
  output.Add(TestOutput("Not Really In Task"));
  output.Add(TestOutput("Action end"));
  return output;
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x