It's well known that there can be some unexpected behavior when one uses ClientBase
- http://geekswithblogs.net/bcaraway/archive/2008/07/06/123622.aspx
- http://geekswithblogs.net/DavidBarrett/archive/2007/11/22/117058.aspx
- http://blog.weminuche.net/2008/08/test-post.html
I guess I come down on the less popular side of the debate, because how ClientBase
public class ServiceClient<T>
{
private ChannelFactory<T> m_factory;
public ServiceClient(ChannelFactory<T> channelFactory)
{
m_factory = channelFactory;
}
public ServiceClient(string endpointConfigurationName) : this(new ChannelFactory<T>(endpointConfigurationName)) { }
public ServiceClient(string endpointConfigurationName, EndpointAddress remoteAddress) : this(new ChannelFactory<T>(endpointConfigurationName, remoteAddress)) { }
public R Execute<R>(Func<T, R> deleg)
{
var chan = m_factory.CreateChannel();
var ico = chan as ICommunicationObject;
R ret;
try
{
ico.Open();
ret = deleg(chan);
}
finally
{
if( ico.State == CommunicationState.Opened )
ico.Close();
else if( ico.State == CommunicationState.Faulted )
ico.Abort();
}
return ret;
}
public void Execute(Action<T> deleg)
{
Execute(c => { deleg(c); return true; });
}
}