I am currently working on a web application with the following setup: Web server (ASP.Net MVC) talking to App server (WCF). The web app supports localization in three languages. As a result, many of the service operations need to return localized (product names, descriptions, …) information as well – depending on the current user’s profile in the web app.
I wanted to solve this as simple as possible (certainly not by adding a languageCode parameter to each operation, or so). A quick experiment lead me to the following…
public class LocalizationBehavior : IClientMessageInspector, // client endpoint IDispatchMessageInspector, // service endpoint IEndpointBehavior { private const string headerName = "localizationHeader"; private const string @namespace = "urn:schemas-jolena-be:localization"; public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) { clientRuntime.MessageInspectors.Add(this); } public object BeforeSendRequest(ref Message request, IClientChannel channel) { request.Headers.Add(MessageHeader.CreateHeader( headerName, @namespace, Thread.CurrentThread.CurrentUICulture.Name)); return null; // no correlation required } public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { endpointDispatcher.DispatchRuntime.MessageInspectors.Add(this); } public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) { var cultureName = request.Headers.GetHeader(headerName, @namespace); Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(cultureName); return null; // no correlation required } public void AfterReceiveReply(ref Message reply, object correlationState) { } public void Validate(ServiceEndpoint endpoint) { } public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) { } public void BeforeSendReply(ref Message reply, object correlationState) { } }
Applying the above behavior to both client and server side endpoints will make sure the CurrentUICulture corresponds to the one of client thread.
What do you think? Makes sense?
Image may be NSFW.
Clik here to view.

Clik here to view.

Clik here to view.

Clik here to view.
