Writing WCF Wrapper and Catching Common Exceptions
Posted on 1250672800|%e %b %Y, %H:%M %Z|agohover under c# code
Suppose you have a WCF proxy class which calls some remote methods. Instead of calling remote methods, lets take the example of a similar class:
public class WcfProxy //calling methods of this class may raise exceptions { //Method with one string parameter public void function1 (string s) { if (String.IsNullOrEmpty (s)) throw new CommunicationException ("Its my nature to throw exceptions..."); Console.WriteLine ("I am in function1"); } //Method with one int parameter public void function2 (int i) { if (i == 0) throw new TimeoutException ("I like to throw exceptions and spoil your day..."); Console.WriteLine ("I am in function2"); } //method with one ref parameter public void function3 (ref string s) { if (String.IsNullOrEmpty (s)) throw new CommunicationException ("Its my nature to throw exceptions..."); s = "function3 executed successfully."; Console.WriteLine ("I am in function3"); } //method with return parameter public string function4 () { Console.WriteLine ("I am in function4"); return "function4 executed successfully."; } }
Now I want to write a wrapper class which will call these function on my behalf and catch the common exceptions related to communication problem. Instead of calling these functions and catching exceptions after every call, we can use generic functions which will call the functions and catch exceptions if raised.
//declare the delegates that are NOT provided by .NET framework public delegate void ActionReference<T> (ref T t1); public delegate T ActionReturn<T> (); public class ProxyWrapper { private readonly WcfProxy proxy = new WcfProxy (); public bool Success { get; set; } public string Message { get; set; } private void ExecuteAndCatch<T> (Action<T> action, T t) { try { action (t); Success = true; } catch (TimeoutException) { Success = false; Message = "Timeout exception raised."; } catch (CommunicationException) { Success = false; Message = "Communication exception raised."; } } private void ExecuteAndCatch<T> (ActionReference<T> action, ref T t) { try { action (ref t); Success = true; } catch (TimeoutException) { Success = false; Message = "Timeout exception raised."; } catch (CommunicationException) { Success = false; Message = "Communication exception raised."; } } private T ExecuteAndCatch<T> (ActionReturn<T> action) { T result = default(T); try { result = action (); Success = true; } catch (TimeoutException) { Success = false; Message = "Timeout exception raised."; } catch (CommunicationException) { Success = false; Message = "Communication exception raised."; } return result; } public void function1 (string s) { ExecuteAndCatch (proxy.function1, s); } public void function2 (int i) { ExecuteAndCatch (proxy.function2, i); } public void function3 (ref string s) { ExecuteAndCatch (proxy.function3, ref s); } public string function4 () { return ExecuteAndCatch<string> (proxy.function4); } //more functions can be called here... }
Now we can test the wrapper class in main method:
public static void Main () { ProxyWrapper w = new ProxyWrapper (); w.function1 (""); Console.WriteLine (w.Success ? "Success!" : "Failed: " + w.Message); w.function2 (0); Console.WriteLine (w.Success ? "Success!" : "Failed: " + w.Message); var s1 = "Initial"; w.function3 (ref s1); Console.WriteLine (w.Success ? "Success! " + s1 : "Failed: " + w.Message); var s2 = w.function4 (); Console.WriteLine (w.Success ? "Success! " + s2 : "Failed: " + w.Message); Console.ReadLine (); }
And here is the output:
Failed: Communication exception raised.
Failed: Timeout exception raised.
I am in function3
Success! function3 executed successfully.
I am in function4
Success! function4 executed successfully.
This solution is developed from the answer given to my question on StackOverflow. You can download the complete source file here.
Next: Cryptic Error Messages
Thanks man been looking for something like this for a few weeks now - Turik from movies online
I am so grateful to read this such a wonderful post.
http://www.nikerbo.com
http://www.nikerko.com
http://www.nikerok.com
http://www.jordanhut.com
http://www.bestmonclerhut.com
Write very good! Back Up.
Discount Sunglasses http://www.bestsunglassesbrand.com
ray ban sunglasses http://www.sunglassko.com
Sunglasses for Glasses http://www.worstsunglassesbrand.com
Discount Nike Sneakers http://www.nikerwo.com
Buy Moncler Jackets http://frbestmoncler.com
cheap moncler coats http://www.wholesaledowncoat.com
cheap moncler jackets http://www.wholesaledowncoat.com
moncler jackets for women http://www.wholesaledowncoat.com
moncler jackets on sale http://www.wholesaledowncoat.com
moncler jackets for sale http://www.wholesaledowncoat.com
moncler down jackets http://www.wholesaledowncoat.com
discount moncler coats http://www.wholesaledowncoat.com
supra footwear shoe http://www.cheapsuprashoeslink.com
surpa sneakers shoe http://www.cheapsuprashoeslink.com
supra shoes online http://www.cheapsuprashoeslink.com
cheap supra shoes http://www.cheapsuprashoeslink.com
supra shoes on sale http://www.cheapsuprashoeslink.com
buy supra shoes http://www.cheapsuprashoeslink.com
supra shoes outlet http://www.cheapsuprashoeslink.com
cheap supra shoe http://www.suprasdiscountstore.com
discount supra shoe http://www.suprasdiscountstore.com
supra shoe outlet http://www.suprasdiscountstore.com
Post preview:
Close preview