2

I have an async WebForms Page (.Net Framework 4.62) and I set the culture in the InitializeCulture method, see below code.

I run some async operation by using the page RegisterAsyncTask method.

The callback for the async operation (on another thread) loses the CurrentCulture. The DefaultThreadCurrentCulture have the right culture, but CurrentThread.CurrentCulture is wrong. Did anyone solve the same trouble?

protected override void InitializeCulture()
{    
    Thread.CurrentThread.CurrentUICulture = currentCulture;
    Thread.CurrentThread.CurrentCulture = currentCulture;
    CultureInfo.CurrentCulture = currentCulture;
    CultureInfo.CurrentUICulture = currentCulture;
    CultureInfo.DefaultThreadCurrentCulture = currentCulture;
    CultureInfo.DefaultThreadCurrentUICulture = currentCulture;
}

This trouble with lost culture began when I updated .Net Framework from 4.51 to 4.62. With WebForms page setting async set to true, I have the default thread culture in the PreRenderComplete event instead of the current culture set in the InitializeCulture method.

It does not depend on whether I register some async operation or not. I know that async operation callback can be on another thread, but I see a bad culture before an async operation is executed, at the start of the async method.

The trouble after the async method execution can be solved with some custom synchronization context, atd...

Frédéric
  • 9,364
  • 3
  • 62
  • 112
mipe
  • 21
  • 2

1 Answers1

1

We had the same issue. We fixed it by adding the following setting:

<add key="appContext.SetSwitch:Switch.System.Globalization.NoAsyncCurrentCulture" value="true" />

See OwinMiddleware doesn't preserve culture change in .net 4.6.* and ASP.NET Middleware doesn't preserve culture anymore.

  • May I ask if you do understand why it fixes the issue? My understanding of the NoAsyncCurrentCulture switch is that it deactivates the flow of culture settings between threads, which is what we try to avoid. I'm kind of lost. – Benjamin Talmard May 01 '20 at 21:50
  • I think that when async=true is activated on a page, a child thread is changing its Culture/UICulture to the default values instead of relying on the "flow of culture". That change is then propagated back to the main thread thanks to the "flow of culture". From my perspective, using the NoAsyncCurrentCulture switch is a hack to fix something else I would like to understand... – Benjamin Talmard May 01 '20 at 22:07
  • @BenjaminTalmard The handling of thread culture has been changed in .net 4.6, see https://learn.microsoft.com/en-us/dotnet/framework/whats-new/#whats-new-in-net-2015 "Changes to the task-based asynchronous pattern (TAP)" and https://learn.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo#Async. According to https://github.com/riganti/dotvvm/issues/330, my best guess is that the setting of the culture doesn't flow back to the 'parent' thread, causing this issue. – Christophe Devos May 23 '20 at 20:31
  • I have not checked, but it looks to me this workaround may well cause issues with culture for MVC or API if your web project mixes them with WebForm, as do ours, which is undergoing a progressive migration to these. Instead, we set the culture to its appropriate value in the `Page_PreRenderComplete` event too, for at least having the "view" (aspx) rendered in the adequate language. – Frédéric May 12 '23 at 09:38