1

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

Community
  • 1
  • 1
ilansch
  • 4,784
  • 7
  • 47
  • 96
  • Which costs are you referring to? Memory? Cpu ticks? Networkload? When are the costs acceptable for you? – rene Aug 04 '13 at 09:34
  • e.g logging client, It can make 50 log message in 1-2 seconds, i dont want to create my wrapper class 50 times in this time period, and i definitely dont want to create the channelfactory class again – ilansch Aug 04 '13 at 09:35
  • If you look at the [Handling Exceptions](http://msdn.microsoft.com/en-us/library/ms789039.aspx) doc I would take an approach where I recreate the wrapper where the recovery strategy says 'Create a new object'. But for most I'll measure/profile to make sure that my changes have the desired effect... – rene Aug 04 '13 at 09:43
  • I am not worried about exception.. i am worried about the way I am supposed to re-create the channel - via .CreateChannel or via .Open() after it close it, and about creating the whole class cost - i want to avoid it.. Thanks though – ilansch Aug 04 '13 at 09:46
  • i think my answer is here http://www.atrevido.net/blog/2007/08/13/Practical+Functional+C+Part+II.aspx but What happen if instead of ChannelFactory.CreateChannel, i just open/close the WCF channel – ilansch Aug 04 '13 at 09:47
  • @ilansch: Was it answered ? – Luciano Jun 04 '14 at 20:25

0 Answers0