Ich verwende automatische Globalisierung auf einer ASP MVC-Website. Es funktioniert gut, bis er einen parallelen Block erreicht:Wie erhalte ich Thread-Kultur in einem parallelen Block korrekt?
public ActionResult Index()
{
// Thread.CurrentThread.CurrentCulture is automatically set to "fr-FR"
// according to the requested "Accept-Language" header
Parallel.Foreach(ids, id => {
// Not every thread in this block has the correct culture.
// Some of them still have the default culture "en-GB"
}) ;
return View()
}
Was ist der beste Weg, der parallel Block erbt die Kultur zu machen? abgesehen von dieser Lösung:
public ActionResult Index()
{
var currentCulture = Thread.CurrentThread.CurrentCulture ;
Parallel.Foreach(ids, id => {
// I don't know if it's threadsafe or not.
Thread.CurrentThread.CurrentCulture = currentCulture ;
}) ;
return View()
}
Wahrscheinlich, einer von ihnen hat die Kultur des Antrags wegen Aufgabe inlining. Die anderen sollten dem Thread-Pool-Verhalten folgen, was auch immer das ist (ich habe es nie herausgefunden). – usr