Tuesday, August 6, 2013

WCF service under Network Load Balancing (NLB)

To use WCF services on multi servers under Network Load Balancing one solution would be utilizing BasicHttpBinding as WCF channels that use this binding are inherently stateless, and terminate their connections when the channel closes.

In message Http header of BasicHttpBinding the Keep-Alive value is true by default which helps to keep persistent connections into services to reuse by sending messages to the same server that improves performance of services. The side effect is the cause of associating clients to specific server which reduces the effectiveness of load balancing. Setting KeepAliveEnabled value to false within a CustomBinding allows NLB functions correctly without sticky session and server affinity.

using (var srv = new SrvClient("BasicHttpLB")) { #region Custom binding to disable [Keep Alive] property of transport Element var wcfsrvCstmBndng = new CustomBinding(svcBinding); var bindingTransportElement = wcfsrvCstmBndng.Elements.Find< HttpTransportBindingElement>(); bindingTransportElement.KeepAliveEnabled = false; // Disable [Keep Alive] property of Transport Element #endregion srv.Endpoint.Binding = wcfsrvCstmBndng; .... //Calling service operations and do what's necessary }

Share/Bookmark

No comments: