Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8883257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:46:16+00:00 2026-06-14T20:46:16+00:00

I am building a set of ASP.Net hosted WebAPI services that must use an

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:46:17+00:00Added an answer on June 14, 2026 at 8:46 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building an asp.net c# application that is heavily reliant on javascript and
I am building an ASP.Net MVC app that will run on a shared hosting
I'm building an Asp.net MVC 2 application. I have an entity called Team that
I'm building an ASP.NET MVC 2 site that uses LINQ to SQL. In one
I'm building a model class in mvc asp.net and I want to use a
I have a simple form on an ASP.NET MVC site that I'm building. This
I am building a library using mvc, mongodb and asp.net membership. When a user
I'm building an asp.net MVC 2 app. I have a list view which lists
I am building an ASP.Net MVC 3 Web application which uses Entity Framework 4.1.
I am building an ASP.NET 3.5 (C#) application and I plan to use the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.