I have a .NET MVC/WebApi app which sets up Culture for the current thread during the ASP.NET AcquireRequestState event (we use custom logic to determine the preferred ui culture for each of our users). While this works for synchronous cases, I'm concerned that it will not work when using async or explicit multi- or background-threading (such as using Task.Run()).
Unlike Thread.CurrentPrincipal, which flows with the logical flow of control, culture seems to simply flow with the thread:
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(FigureOutUsersCulture());
Task.Run(() => Thread.CurrentThread.CurrentUICulture.Dump()); // en-US
What's the most robust way to set up culture for such an application?
Note: I am not excited about any answer that requires every developer who uses async/await or other threading to remember to initialize the culture. If possible, I'd like something robust across async/await. If this is truly impossible, I'd also accept that answer of course.
EDIT: I understand that using Task.Run() has issues in ASP.NET if you don't wait for the task during a request thread. That said, I am trying to write the infrastructure to set this up in the most robust way possible. I don't know what other developers will do, so if I can be set up for it, I'd like to be.
EDIT: A note about the posted "possible duplicates". I agree that both of these questions are related, but they are not the same! One asks about setting one culture for the whole app, which is the opposite of what I'm trying to do. The other tackles setting culture for MVC3-style async controllers, which is a small (and, to me, largely irrelevant) subset of what I'd like to accomplish.