private void GetChannelAsyncInternal <TContract>(ContractInfo contract, EventHandler <ChannelEstablishedEventArgs> callback) where TContract : class
        {
            IProxyChannelManager <TContract> proxyChannelManager = GetProxyChannelManager <TContract>(contract.Key);

            if (proxyChannelManager == null)
            {
                lock (_proxyChannelManagerContainerLocker)
                {
                    proxyChannelManager = GetProxyChannelManager <TContract>(contract.Key);
                    if (proxyChannelManager == null)
                    {
                        proxyChannelManager = ProxyChannelManagerFactory.Create <TContract>(contract);
                        RegisterProxyChannelManager(proxyChannelManager, contract.Key);
                    }
                }
            }

            SaveAsyncChannelEstablishedCallback(contract.Key, callback);
            proxyChannelManager.GetChannelAsync();
        }
        private TContract GetChannelInternal <TContract>(ContractInfo contract) where TContract : class
        {
            // get the proxy channel manager by the specified contract type and key
            IProxyChannelManager <TContract> proxyChannelManager = GetProxyChannelManager <TContract>(contract.Key);

            if (proxyChannelManager == null)
            {
                lock (_proxyChannelManagerContainerLocker)
                {
                    // double check the channel manager value
                    proxyChannelManager = GetProxyChannelManager <TContract>(contract.Key);
                    if (proxyChannelManager == null)
                    {
                        proxyChannelManager = ProxyChannelManagerFactory.Create <TContract>(contract);
                        RegisterProxyChannelManager(proxyChannelManager, contract.Key);
                    }
                }
            }

            return(proxyChannelManager.GetChannel());
        }
        /// <summary>
        /// 获取指定服务契约类型的所有通道连接
        /// </summary>
        /// <typeparam name="TContract">服务契约类型</typeparam>
        /// <returns>指定服务契约类型的所有通道连接</returns>
        public IList <TContract> GetChannels <TContract>() where TContract : class
        {
            List <TContract> list = new List <TContract>();

            IList <ServiceEndpoint> endpoints = EndpointProvider.GetEndpoints <TContract>();
            List <IProxyChannelManager <TContract> > channelManagerList = new List <IProxyChannelManager <TContract> >();
            List <ServiceEndpoint> channelExistEndpoints = new List <ServiceEndpoint>();

            List <object> proxyChannelManagers = new List <object>(_proxyChannelManagerContainer.Values);

            foreach (var item in proxyChannelManagers)
            {
                if (item is ProxyChannelManager <TContract> )
                {
                    IProxyChannelManager <TContract> channelManager = item as IProxyChannelManager <TContract>;
                    if (channelManager != null)
                    {
                        channelManagerList.Add(channelManager);

                        foreach (ServiceEndpoint endpoint in endpoints)
                        {
                            if (endpoint.Address.Uri.Host == channelManager.ContractInfo.HostName)
                            {
                                channelExistEndpoints.Add(endpoint);
                                break;
                            }
                        }
                    }
                }
            }

            foreach (ServiceEndpoint endpoint in channelExistEndpoints)
            {
                endpoints.Remove(endpoint);
            }

            if (endpoints.Count > 0)
            {
                // create proxy channel manager for those endpoints who have not been created
                foreach (ServiceEndpoint endpoint in endpoints)
                {
                    ContractInfo contract = new ContractInfo(typeof(TContract), endpoint.Address.Uri.Host);
                    IProxyChannelManager <TContract> proxyChannelManager = GetProxyChannelManager <TContract>(contract.Key);
                    if (proxyChannelManager == null)
                    {
                        lock (_proxyChannelManagerContainerLocker)
                        {
                            proxyChannelManager = GetProxyChannelManager <TContract>(contract.Key);
                            if (proxyChannelManager == null)
                            {
                                proxyChannelManager = ProxyChannelManagerFactory.Create <TContract>(contract);
                                RegisterProxyChannelManager(proxyChannelManager, contract.Key);
                            }
                        }
                    }

                    if (proxyChannelManager != null)
                    {
                        channelManagerList.Add(proxyChannelManager);
                    }
                }
            }

            foreach (ProxyChannelManager <TContract> channelManager in channelManagerList)
            {
                list.Add(channelManager.GetChannel());
            }

            return(list);
        }