Pregunta

I have a web application that's using a set of web services. When an exception is caught on a web service and sent back to the client I need that message to be displayed in the language currently set on the web app.

My first thought was to send a SoapHeader containing the web app's CurrentUICulture and change the culture like this:

CultureInfo culture;
culture = CultureInfo.CreateSpecificCulture(CultureHeader.currentCulture);
System.Threading.Thread.CurrentThread.CurrentCulture = culture;
System.Threading.Thread.CurrentThread.CurrentUICulture = culture;

I now wonder if this will affect all users of the web service or just the current request?

¿Fue útil?

Solución

I now wonder if this will affect all users of the web service or just the current request?

It will affect all requests serviced by the current thread (until something else changes the properties). However it will not affect requests serviced by other threads.

Remember ASP.NET uses a pool of threads to allow multiple requests to be serviced concurrently, however when debugging you are usually only dealing with a single request at a time so this is not immediately obvious.

Of course, if you are using ASP.NET's async support, allowing different pool threads to start services the request and start async operations, but then when those async operations complete the thread pool is used again sometimes leading to a different thread completing the operation then things could get interesting if one thread has one culture set and another thread has a different one.

Summary: good practice is to never rely on the implicit locale set on the current thread: all culture dependent operations (including all calls, direct or indirect, to ToString) should explicitly pass the correct culture.

Even better is to explicitly decide and use specific formatting (eg. all dates and times in ISO-8601) for each data type where culture makes a difference, and thus clients don't need to deal with guessing the correct interpretation (eg. some people think today is not 1/11/2013).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top