It was during the Virtual Tech Days when I was presenting the "Exception Handling In WCF" session, one attendee wanted on how to use the fault reason to have multilingual / multicultural exceptions being handled and shown without any extra code. And so I promised him that I would post an article on the same and here it is now.
To start with, FaultReason
accepts a list of FaultReasonText
which can be basically said to have exception messages for multiple cultures. The same can be added as below:
List reasonText = new List();
reasonText.Add(new FaultReasonText("Good Morning World", new CultureInfo("en-US")));
reasonText.Add(new FaultReasonText("Bon matin mondiale", new CultureInfo("fr-FR")));
reasonText.Add(new FaultReasonText("????? ???? ???", new CultureInfo("he-IL")));
reasonText.Add(new FaultReasonText("??? ???????? ??????", new CultureInfo("hi-IN")));
Now all I have to do is to set the culture in the UI to the current thread and call the method to get the current culture and bind the property as below:
ex.Reason.GetMatchingTranslation().Text;
Where ex
will be the FaultException
. Always remember that this works only with SOAP 1.2 and therefore you will need to write a custom binding as mentioned in this sample.