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.

rating: 0+x
Add a New Comment
or Sign in as Wikidot user
(will not be published)
- +