Skip to content

Instantly share code, notes, and snippets.

@JoshClose
Created November 15, 2011 15:08
Show Gist options
  • Save JoshClose/1367292 to your computer and use it in GitHub Desktop.
Save JoshClose/1367292 to your computer and use it in GitHub Desktop.
How Safe is the Using Block?
var proxy = new MyProxy();
try
{
// Do some work.
proxy.Close();
}
catch( CommunicationException ex )
{
proxy.Abort();
}
catch( TimeoutException ex )
{
proxy.Abort();
}
catch( Exception ex )
{
proxy.Abort();
throw;
}
public abstract class ProxyBase<TChannel> : ClientBase<TChannel>, IDisposable where TChannel : class
{
protected void CheckDisposed()
{
if( disposed )
{
throw new ObjectDisposedException( GetType().Name );
}
}
public void Dispose()
{
Dispose( true );
GC.SuppressFinalize( this );
}
protected virtual void Dispose( bool disposing )
{
if( !disposed )
{
if( disposing )
{
try
{
base.Close();
}
catch( CommunicationException ex )
{
base.Abort();
}
catch( TimeoutException ex )
{
base.Abort();
}
catch( Exception ex )
{
base.Abort();
throw;
}
}
disposed = true;
}
}
}
using( var proxy = new MyProxy() )
{
// Do some work.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment