Created
November 15, 2011 15:08
-
-
Save JoshClose/1367292 to your computer and use it in GitHub Desktop.
How Safe is the Using Block?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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