title: ASP.NET CurrentUICulture behaves differently in Task and WebAPI deferred result
body: <p>I'm working on an ASP.NET Web API project in which I had an issue where the child <code>Task</code>s which run asynchronously were not inheriting the the current culture of the parent thread; i.e., even after setting the <code>Thread.CurrentThread.CurrentUICulture</code> in the controller constructor, any <code>Task</code> created within the action was having the invariant culture by default unless I set it separately. This seems to have fixed in framework > 4.0 and was working fine after upgrading the target framework from <code>3.5.2</code> to <code>4.6.2</code> <a href="https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/task-based-asynchronous-programming" rel="nofollow noreferrer">as mentioned here</a>.</p>
  
  <blockquote>
    <p>Starting with apps that target the .NET Framework 4.6, the calling thread's culture is inherited by each task, even if the task runs asynchronously on a thread pool thread.</p>
  </blockquote>
  
  <p>Now that the culture inheritance in the Tasks are working as expected, I'm facing another weird issue which was <strong>not present in 4.5.2</strong>. If the WebApi action result is of type <code>IEnumerable</code>, then the result will have the default culture in it. Maybe the issue could be better explained using a sample code snippet:</p>
  
  <pre><code>public SampleController()
  {
      System.Threading.Thread.CurrentThread.CurrentUICulture =
          new System.Globalization.CultureInfo("ar-AE");
  }
  
  [Route]
  public IHttpActionResult Get()
  {
      IEnumerable&lt;Employee&gt; employeesDeferred = BuildEmployees(true);
      return Ok(employeesDeferred);
  }
  
  private IEnumerable&lt;Employee&gt; BuildEmployees(bool isDeferred)
  {
      var employees = Enumerable.Range(0, 2)
          .Select(count =&gt; new Employee
          {
              Id = count++,
              Culture = Thread.CurrentThread.CurrentUICulture.Name
          });
  
      if (isDeferred) { return employees; }
  
      return employees.ToList();
  }
  </code></pre>
  
  <p>Please note that I'm setting the UI culture as "ar-AE" in the constructor. PFB the results:</p>
  
  <ul>
  <li>framework 4.5.2 - The <code>Culture</code> of all employees will be ar-AE as expected</li>
  <li>framework 4.6.2 - The <code>Culture</code> of all employees will be en-US (default)</li>
  <li>framework 4.6.2 by doing a <code>.ToList()</code> in the result (calling <code>BuildEmployees(false)</code> instead of <code>BuildEmployees(true)</code> in the above code) - The <code>Culture</code> of all employees will be ar-AE as expected</li>
  </ul>
  
  <p>I did had some research and saw answers which suggests to set the <code>NoAsyncCurrentCulture</code> switch, for eg, by adding the switch in the app settings <code>&lt;add key="appContext.SetSwitch:Switch.System.Globalization.NoAsyncCurrentCulture" value="true" /&gt;</code> - but this will kinda rollback to the behavior we had in &lt;4.0 and will cause the issue with the culture inheritance in the Tasks as mentioned in the first para.</p>
  
  <p>What am I missing?</p>
  
  <p>Update: Verified that the context culture is not getting inherited in the thread where the (JSON/XML) serialization is happening in Web API.</p>