Click here to Skip to main content
15,891,864 members
Articles / Programming Languages / C#

Multi Language / Multicultural Exception Handling In WCF Using FaultReason

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
10 Jan 2011CPOL 13K   1   1
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)


Written By
Software Developer (Senior) Microsoft Corporation
India India
I work for Microsoft on MS technologies for application development. My interests include .net, WCF, Azure, Windows Phone, ASP.net, SL, WCF, WPF and many more.

You can visit my site at http://www.jebarson.info

Follow me on twitter @jebarson007

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 78509016-Jun-12 5:58
Member 78509016-Jun-12 5:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.