Skip to content

Instantly share code, notes, and snippets.

@pregress
Created October 10, 2012 07:41
Show Gist options
  • Save pregress/3863804 to your computer and use it in GitHub Desktop.
Save pregress/3863804 to your computer and use it in GitHub Desktop.
Indisposable: WCF Gotcha #1
public delegate void UseServiceDelegate<T>(T proxy);
public interface IService<T>
{
void Use(UseServiceDelegate<T> codeBlock);
}
public class Service<T> : IService<T>
{
private readonly ChannelFactory<T> _channelFactory;
public Service<T>(string endpoint)
{
_channelFactory = new ChannelFactory<T>(endpoint);
}
public void Use(UseServiceDelegate<T> codeBlock)
{
var proxy = (IClientChannel) _channelFactory.CreateChannel();
var success = false;
try
{
codeBlock((T)proxy);
proxy.Close();
success = true;
}
catch (CommunicationException ex)
{
//Logging
}
catch (TimeoutException ex)
{
//Logging
}
catch (Exception ex)
{
//Logging
}
finally
{
if (!success)
{
//Logging
proxy.Abort();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment