Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Multi Language / Multicultural Exception Handling In WCF Using FaultReason

5.00/5 (2 votes)
10 Jan 2011CPOL 13.2K  
Multi Language / Multicultural Exception Handling In WCF Using FaultReason

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:

C#
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:

C#
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)