/// <summary>
 /// Gets the WCF service binding for the specified protocol and throttle settings.
 /// </summary>
 /// <param name="protocol">The protocol.</param>
 /// <param name="maxConnections">The max connections.</param>
 /// <param name="leaseTimeout">The lease timeout.</param>
 /// <returns>A <see cref="Binding"/> object.</returns>
 public static Binding GetBinding(BusinessServicesProtocol protocol, int? maxConnections, int? leaseTimeout)
 {
     return GetBinding(protocol, null, maxConnections, leaseTimeout);
 }
        /// <summary>
        /// Gets the WCF service binding for the specified protocol, security mode, and throttle settings.
        /// </summary>
        /// <param name="protocol">The protocol.</param>
        /// <param name="securityMode">The security mode.</param>
        /// <param name="maxConnections">The max connections.</param>
        /// <param name="leaseTimeout">The lease timeout.</param>
        /// <returns>A <see cref="Binding"/> object.</returns>
        public static Binding GetBinding(BusinessServicesProtocol protocol, SecurityMode? securityMode, int? maxConnections, int? leaseTimeout)
        {
            if (protocol == BusinessServicesProtocol.WSHttp)
            {
                WSHttpBinding binding = new WSHttpBinding();

                binding.Security.Mode = securityMode ?? ConfigSettingsReader.WSHttpWcfSecurityMode();
                binding.MaxReceivedMessageSize = ConfigSettingsReader.MaxSendReceiveSize();
                binding.ReaderQuotas.MaxBytesPerRead = ConfigSettingsReader.MaxSendReceiveSize();
                binding.ReaderQuotas.MaxDepth = ConfigSettingsReader.MaxSendReceiveSize();
                binding.ReaderQuotas.MaxArrayLength = ConfigSettingsReader.MaxSendReceiveSize();
                binding.ReaderQuotas.MaxStringContentLength = ConfigSettingsReader.MaxSendReceiveSize();
                binding.ReceiveTimeout = ConfigSettingsReader.ServicesClientTimeout();
                binding.SendTimeout = ConfigSettingsReader.ServicesClientTimeout();
                binding.OpenTimeout = ConfigSettingsReader.ServicesClientTimeout();
                binding.CloseTimeout = ConfigSettingsReader.ServicesClientTimeout();
                binding.ReliableSession.InactivityTimeout = ConfigSettingsReader.ServicesClientTimeout();

                return binding;
            }
            else if (protocol == BusinessServicesProtocol.NetTcp)
            {
                NetTcpBinding binding = new NetTcpBinding();

                binding.Security.Mode = securityMode ?? ConfigSettingsReader.NetTcpWcfSecurityMode();
                binding.MaxReceivedMessageSize = ConfigSettingsReader.MaxSendReceiveSize();
                binding.ReaderQuotas.MaxBytesPerRead = ConfigSettingsReader.MaxSendReceiveSize();
                binding.ReaderQuotas.MaxDepth = ConfigSettingsReader.MaxSendReceiveSize();
                binding.ReaderQuotas.MaxArrayLength = ConfigSettingsReader.MaxSendReceiveSize();
                binding.ReaderQuotas.MaxStringContentLength = ConfigSettingsReader.MaxSendReceiveSize();
                binding.PortSharingEnabled = ConfigSettingsReader.PortSharingEnabled();
                binding.ReceiveTimeout = ConfigSettingsReader.ServicesClientTimeout();
                binding.SendTimeout = ConfigSettingsReader.ServicesClientTimeout();
                binding.OpenTimeout = ConfigSettingsReader.ServicesClientTimeout();
                binding.CloseTimeout = ConfigSettingsReader.ServicesClientTimeout();
                binding.ReliableSession.InactivityTimeout = ConfigSettingsReader.ServicesClientTimeout();

                if (maxConnections != null)
                    binding.MaxConnections = maxConnections.Value;

                if (leaseTimeout != null)
                    binding.CreateBindingElements().Find<TcpTransportBindingElement>().ConnectionPoolSettings.LeaseTimeout = new TimeSpan(0, 0, leaseTimeout.Value);

                return binding;
            }
            else
            {
                NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);

                binding.MaxReceivedMessageSize = BusinessServicesConstants.MaxSendReceiveSize;
                binding.ReaderQuotas.MaxBytesPerRead = BusinessServicesConstants.MaxSendReceiveSize;
                binding.ReaderQuotas.MaxDepth = BusinessServicesConstants.MaxSendReceiveSize;
                binding.ReaderQuotas.MaxArrayLength = BusinessServicesConstants.MaxSendReceiveSize;
                binding.ReaderQuotas.MaxStringContentLength = BusinessServicesConstants.MaxSendReceiveSize;
                binding.ReceiveTimeout = new TimeSpan(0, BusinessServicesConstants.ServicesClientTimeout, 0);
                binding.SendTimeout = new TimeSpan(0, BusinessServicesConstants.ServicesClientTimeout, 0);
                binding.OpenTimeout = new TimeSpan(0, BusinessServicesConstants.ServicesClientTimeout, 0);
                binding.CloseTimeout = new TimeSpan(0, BusinessServicesConstants.ServicesClientTimeout, 0);

                if (maxConnections != null)
                    binding.MaxConnections = maxConnections.Value;

                return binding;
            }
        }
        /// <summary>
        /// Gets the WCF service endpoint address for the specified host, port, services name, protocol, and contract.
        /// </summary>
        /// <param name="host">The host.</param>
        /// <param name="port">The port.</param>
        /// <param name="servicesName">Name of the services.</param>
        /// <param name="protocol">The protocol.</param>
        /// <param name="contractType">Type of the contract.</param>
        /// <returns>The WCF service endpoint.</returns>
        public static string GetEndpointAddress(string host, int port, string servicesName, BusinessServicesProtocol protocol, Type contractType)
        {
            if (string.IsNullOrEmpty(host))
            {
                host = Environment.MachineName;
            }

            if (protocol == BusinessServicesProtocol.WSHttp)
            {
                if (!string.IsNullOrEmpty(servicesName))
                {
                    return string.Format(CultureInfo.InvariantCulture, "http://{0}:{1}/{2}/{3}.svc", host, port, servicesName, contractType.FullName);
                }
                else
                {
                    return string.Format(CultureInfo.InvariantCulture, "http://{0}:{1}/{2}.svc", host, port, contractType.FullName);
                }
            }
            else if (protocol == BusinessServicesProtocol.NetTcp)
            {
                if (!string.IsNullOrEmpty(servicesName))
                {
                    return string.Format(CultureInfo.InvariantCulture, "net.tcp://{0}:{1}/{2}/{3}.svc", host, port, servicesName, contractType.FullName);
                }
                else
                {
                    return string.Format(CultureInfo.InvariantCulture, "net.tcp://{0}:{1}/{2}.svc", host, port, contractType.FullName);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(servicesName))
                {
                    return string.Format(CultureInfo.InvariantCulture, "net.pipe://{0}/{1}/{2}", host, servicesName, contractType.FullName);
                }
                else
                {
                    return string.Format(CultureInfo.InvariantCulture, "net.pipe://{0}/{1}", host, contractType.FullName);
                }
            }
        }