Advertisement
Advertisement
| 06.23.2008 at 10:32AM PDT, ID: 23508365 |
|
[x]
Attachment Details
|
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: |
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.ServiceModel;
namespace ConsoleApplication1
{
public delegate void SafeProxyInvocationHandler(out ICommunicationObject proxy);
public static class SafeProxyHandler
{
public static Exception Invoke(SafeProxyInvocationHandler del)
{
return Invoke(null, del);
}
public static Exception Invoke(string messageTemplate,
SafeProxyInvocationHandler del)
{
if (del == null)
throw new ArgumentException("Expected delegate instance for handler to invoke!");
ICommunicationObject proxy = null;
try
{
del(out proxy);
HandleClose(proxy);
return null;
}
catch (TimeoutException exception)
{
HandleException(proxy, exception, messageTemplate);
return exception;
}
catch (FaultException exception)
{
HandleException(proxy, exception, messageTemplate);
return exception;
}
catch (CommunicationException exception)
{
HandleException(proxy, exception, messageTemplate);
return exception;
}
catch (Exception exception)
{
HandleException(proxy, exception, messageTemplate);
return exception;
}
}
private static void HandleClose(ICommunicationObject proxy)
{
if (proxy != null
&& proxy.State != CommunicationState.Closed
&& proxy.State != CommunicationState.Closing
&& proxy.State != CommunicationState.Faulted)
proxy.Close();
}
private static void HandleException(ICommunicationObject proxy,
Exception e,
string messageTemplate)
{
AbortClient(proxy);
ReportException(e, messageTemplate);
}
private static void ReportException(Exception e, string template)
{
Trace.WriteLine(FormatMessage(template, e.GetType().Name));
}
private static string FormatMessage(string message, params string[] args)
{
return args == null || args.Length == 0 ? message : string.Format(message, args);
}
private static void AbortClient(ICommunicationObject proxy)
{
if (proxy != null)
proxy.Abort();
}
}
}
|