using System; using System.Collections.Generic; using System.ServiceModel; namespace LogicaCMG.ServiceAccess { /// /// Generic helper class for a WCF service proxy. /// /// The type of WCF service proxy to wrap. /// The type of WCF service interface to wrap. public class ServiceProxyHelper : IDisposable where TProxy : ClientBase, new() where TChannel : class { /// /// Private instance of the WCF service proxy. /// private TProxy _proxy; /// /// Gets the WCF service proxy wrapped by this instance. /// protected TProxy Proxy { get { if (_proxy != null) { return _proxy; } else { throw new ObjectDisposedException("ServiceProxyHelper"); } } } /// /// Constructs an instance. /// protected ServiceProxyHelper() { _proxy = new TProxy(); } /// /// Disposes of this instance. /// public void Dispose() { try { if (_proxy != null) { if (_proxy.State != CommunicationState.Faulted) { _proxy.Close(); } else { _proxy.Abort(); } } } catch (CommunicationException) { _proxy.Abort(); } catch (TimeoutException) { _proxy.Abort(); } catch (Exception) { _proxy.Abort(); throw; } finally { _proxy = null; } } } }