If you execute the following code in ASP.NET MVC, you can see in Debugging Window that it will correctly restore the thread's culture after await
, even if ManagedThreadId changes:
public async Task<ActionResult> Index()
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE");
Debug.WriteLine(Thread.CurrentThread.ManagedThreadId);
Debug.WriteLine(Thread.CurrentThread.CurrentUICulture);
await SomeMethod();
Debug.WriteLine(Thread.CurrentThread.ManagedThreadId);
Debug.WriteLine(Thread.CurrentThread.CurrentUICulture);
return View();
}
private async Task SomeMethod()
{
await Task.Delay(100).ConfigureAwait(false);
}
Then I just move ConfigureAwait(false) from SomeMethod() to Index(), except for this, it's the same code as above:
public async Task<ActionResult> Index()
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE");
Debug.WriteLine(Thread.CurrentThread.ManagedThreadId);
Debug.WriteLine(Thread.CurrentThread.CurrentUICulture);
await SomeMethod().ConfigureAwait(false);
Debug.WriteLine(Thread.CurrentThread.ManagedThreadId);
Debug.WriteLine(Thread.CurrentThread.CurrentUICulture);
return View();
}
private async Task SomeMethod()
{
await Task.Delay(100);
}
Now it doesn't restore my culture but always set it to new CultureInfo("en-US")
. But I expect that using both methods, the result must be the same. It's absolutely unclear, why it's happening.