Created
October 4, 2013 15:19
-
-
Save djfr/6827699 to your computer and use it in GitHub Desktop.
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 class WcfService<T> : IDisposable where T : class | |
{ | |
private readonly object _lockObject = new object(); | |
private bool _disposed; | |
private ChannelFactory<T> _factory; | |
private T _channel; | |
internal WcfService() | |
{ | |
_disposed = false; | |
} | |
internal virtual T Channel | |
{ | |
get | |
{ | |
if (_disposed) | |
{ | |
throw new ObjectDisposedException("Resource WcfService<" + typeof(T) + "> has been disposed"); | |
} | |
lock (_lockObject) | |
{ | |
if (_factory == null) | |
{ | |
_factory = new ChannelFactory<T>("*"); // First qualifying endpoint from the config file | |
_channel = _factory.CreateChannel(); | |
} | |
} | |
return _channel; | |
} | |
} | |
public void Dispose() | |
{ | |
Dispose(true); | |
GC.SuppressFinalize(this); | |
} | |
internal void Dispose(bool disposing) | |
{ | |
if (_disposed) | |
{ | |
return; | |
} | |
if (!disposing) | |
{ | |
return; | |
} | |
lock (_lockObject) | |
{ | |
if (_channel != null) | |
{ | |
try | |
{ | |
((IClientChannel)_channel).Close(); | |
} | |
catch (Exception) | |
{ | |
((IClientChannel)_channel).Abort(); | |
} | |
} | |
if (_factory != null) | |
{ | |
try | |
{ | |
_factory.Close(); | |
} | |
catch (Exception) | |
{ | |
_factory.Abort(); | |
} | |
} | |
_channel = null; | |
_factory = null; | |
_disposed = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment