What's the best way to handle Culture in a .NET MVC / WebApi application using async?

I have a .NET MVC / WebApi application that sets the culture for the current thread during the ASP.NET AcquireRequestState event (we use custom logic to define the preferred ui culture for each of our users). While this works for synchronous cases, I am concerned that it will not work when using async or an explicit multipoint or background thread (such as with Task.Run ()).

Unlike Thread.CurrentPrincipal, which flows with a logical flow of control, it seems like the culture is just flowing with the flow:

Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(FigureOutUsersCulture());
Task.Run(() => Thread.CurrentThread.CurrentUICulture.Dump()); // en-US

      

What's the most reliable way to set up a culture for such an application?

Note. I don't care about any answer that requires every developer who uses async / await or other threads to remember to initialize the culture. If possible I would like something to be robust in async / await. If this is really not possible, I would also agree with this answer.

EDIT: I understand that using Task.Run () has problems in ASP.NET if you don't wait for a task while the request is flowing. However, I am trying to write a framework to install it in the most reliable way. I don't know what other developers will be doing, so if I can customize it I would like to be.

EDIT: A note on "possible duplicates" . I agree that both of these questions are related, but they are not the same! One of them asks to set one culture for the whole application, which is the opposite of what I am trying to do. Another way of setting culture for MVC3 asynchronous controllers, which is a small (and for me, largely irrelevant) subset of what I would like to accomplish.

+3


source to share


2 answers


Kyle's advice on delegating culture-related jobs to other threads (or scheduled in the application pool) is reasonable. If the job needs to know about the culture, pass it as a parameter.

Just using await

without any fancy tricks like ConfigureAwait(false)

will persist both the HttpContext and the thread culture in the continuations, so I would go with that *. I haven't seen any performance issues of the AspNetSynchronizationContext ( fooobar.com/questions/18701 / ... ) and the reason you chose C # is to take advantage of the performance benefits.



* If you are writing library code, you may want to consider ConfigureAwait(false)

. Again, you will ensure that all the "normal environment" information (Context.Current, CurrentUICulture, etc.) is passed as parameters. There are also interesting tricks like this here on CultureAwaiter , maybe you could do something. And you might want to read the costs and decide for yourself if implementing these optimizations is worth your time or not.

+2


source


Given the "possible duplicate" comments, I'm not sure there is a better answer than what EZI said. If you want to create a new stream, you need to talk about this culture.



A better question might be why do you need culture in other threads? Make these streams work for you, and then when you return to the main stream, you can convert the information to strings or whatever. This may not be appropriate as I don't know what exactly threads are doing.

0


source







All Articles