Since the renderer dispatcher is not a thread dispatcher, and multiple InvokeAsync invocations might use different threads, is there any way I can guarantee CultureInfo.CurrentUICulture will be the same on all invocations?
My application's Culture is dynamic (it's stored on session and does not come from the asp's localization middleware, plus it can be changed dynamically within the SignalR circuit and does not need to open a new http connection) and the current culture is stored on each component, however events called from a different circuit (which might have a different culture) might want to change something in a component instance rendered in other users.
I store the component's CultureInfo in the component itself, but I'm trying to avoid having to do all the ToString(Culture) or @bind-X:culture="@Culture" on the templates, since it's both verbose and a bit cumbersome (and prone to forgetness and mistakes), so whenever the component's culture changes, I do something like:
await InvokeAsync(() => CultureInfo.CurrentUICulture = newCulture));
Is this enough so that other calls to that component's InvokeAsync (and the renderer dispatcher) will correctly get the thread's culture?
It does seem to work, but I haven't seen this guaranteed in any documentation (nor InvokeAsync guaranteeing local thread values will be kept between different invocations)
Update
I made a simple test page (I've added the source code here: https://gist.github.com/javiercampos/1152da404c3004d7fbaac8e503425e4a ), and the CurrentCulture/CurrentUICulture definitely doesn't:
- Flow with async on the component's context
- Change at all if you change it on a
InvokeAsync()
So tough luck here, unless someone adds that flow to the actual renderer synchronization context
I've made a request for the feature: https://github.com/dotnet/aspnetcore/issues/35965