Created
October 10, 2012 07:41
-
-
Save pregress/3863804 to your computer and use it in GitHub Desktop.
Indisposable: WCF Gotcha #1
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 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