I am using WCF infra in my Managed applications.
I have wrapper classes for the clients that looks like:
public class ServiceClient<TService>
where TService : class
{
private TService _channel;
private ChannelFactory<TService> _channelFactory;
private readonly object _syncLock = new object();
private readonly Binding _binding;
private readonly string _uri;
//I have sets of constructors, i get the endpoint and binding type for each client in constructing.
public TService Channel { get { Initiliaze(); return _channel; } }
private void Initiliaze()
{
lock (_syncLock)
{
if (_channel != null) return;
//Create channel factory with binding and address
_channelFactory = new ChannelFactory<TService>(_binding,_uri);
//add additional behaviors if they exist
if (_applyDefaultBehavior)
{
_channelFactory.Endpoint.Behaviors.Add(new ClientEndpointBehavior<TService>(_container));
}
//register client on channel events
_channelFactory.Opened += OnChannelFactoryOpened;
_channelFactory.Closed += OnChannelFactoryClosed;
_channelFactory.Faulted += OnChannelFactoryFaulted;
_channel = _channelFactory.CreateChannel();
}
}
The rest is the Dispose and its less important right now.
So far i when i used ServiceClient - I didnt use using ( .. ServiceClient ...) But I didnt either Open / Close the WCF Channels.
The problem i am dealing, I need to open/close the channel on each call, I dont want to re-create the entire ServiceClient wrapper on each call, because the cost of creating a ChannelFactory with these bindings. I just want to open.close the channel.
Can i do:
((IChannel)_channel).Close();
((IChannel)_channel).Open();
before each operation ? Or i need to do:
((IChannel)_channel).Close();
_channel = _channelFactory.CreateChannel();
In order to re-create the channel ?
I think the best solution is to make a public function in my wrapper as the sample in https://stackoverflow.com/a/573877/1426106