Search code examples
wcfnettcpbinding

How to set the leaseTimeout setting programmaticaly?


We have a WCF service (NetTcpBinding) that sits behind a load balancer. I've read that in order to avoid "stickyniss" I have lower the LeaseTime the channels get in the channel pool.

I've only found samples how to set this value using the config file, but I would like to set it programmaticaly, any pointers?


Solution

  • You can access the LeaseTimeout property via the TcpTransportBindingElement, through the ConnectionPoolSettings property:

    TcpTransportBindingElement tcpBE = new TcpTransportBindingElement();
    tcpBE.ConnectionPoolSettings.LeaseTimeout = TimeSpan.FromSeconds(1);
    

    If you have a NetTcpBinding object, you'll need to first convert it into a CustomBinding, then access the binding element. The example below shows one way of doing this.

    NetTcpBinding myOriginalBinding = CreateBinding();
    CustomBinding newBinding = new CustomBinding(myOriginalBinding);
    TcpTransportBindingElement tcpBE = newBinding.Elements.Find<TcpTransportBindingElement>();
    tcpBE.ConnectionPoolSettings.LeaseTimeout = TimeSpan.FromSeconds(1);