Ejemplo n.º 1
0
        internal static TransientChannelTicket CreateTransientChannel(LogicalChannel logicalChannel)
        {
            DiagnosticUtility.DebugAssert(logicalChannel != null, "logical channel cannot be null");

            ChannelFactory factory       = null;
            IChannel       channel       = null;
            bool           channelOpened = false;

            try
            {
                factory       = ChannelManagerHelpers.CreateChannelFactory(logicalChannel.ConfigurationName, logicalChannel.ContractType);
                channel       = ChannelManagerHelpers.CreateChannel(logicalChannel.ContractType, factory, logicalChannel.CustomAddress);
                channelOpened = true;
            }
            finally
            {
                if (!channelOpened)
                {
                    if (channel != null)
                    {
                        ChannelManagerHelpers.CloseCommunicationObject(channel);
                    }
                    if (factory != null)
                    {
                        ChannelManagerHelpers.CloseCommunicationObject(factory);
                    }
                }
            }

            return(new TransientChannelTicket(channel, factory));
        }
        public ChannelManager(ChannelPoolSettings settings, IList <ServiceEndpoint> endpoints)
        {
            this.channelPool  = new PooledChannelPool(settings);
            this.factoryCache = new Dictionary <EndpointAddress, ChannelFactoryReference>();

            this.newChannels = new List <PooledChannel>();

            this.endpoints        = new Dictionary <EndpointAddress, ServiceEndpoint>();
            this.endpointMappings = Hashtable.Synchronized(new Hashtable());

            if (endpoints != null)
            {
                foreach (ServiceEndpoint endpoint in endpoints)
                {
                    if (endpoint != null)
                    {
                        EndpointAddress cacheAddress = null;
                        if (endpoint.Contract.ContractType != null)
                        {
                            cacheAddress = ChannelManagerHelpers.BuildCacheAddress(endpoint.Name, endpoint.Contract.ContractType);
                        }
                        else
                        {
                            cacheAddress = ChannelManagerHelpers.BuildCacheAddress(endpoint.Name, endpoint.Contract.Name);
                        }
                        this.endpoints.Add(cacheAddress, endpoint);
                    }
                }
            }
        }
Ejemplo n.º 3
0
 protected override void Close()
 {
     if (this.channel != null)
     {
         ChannelManagerHelpers.CloseCommunicationObject(this.channel);
         this.channel = null;
     }
     if (this.factory != null)
     {
         ChannelManagerHelpers.CloseCommunicationObject(this.factory);
         this.factory = null;
     }
 }
        public PooledChannel TakeChannel(string endpointName, Type contractType, string customAddress, out ChannelPoolKey key)
        {
            EndpointAddress cacheAddress = ChannelManagerHelpers.BuildCacheAddress(endpointName, contractType);
            Uri             via          = (customAddress != null) ? new Uri(customAddress) : defaultViaUri;

            if (this.closed)
            {
                key = null;
                return(null);
            }

            this.endpointMappings[cacheAddress] = new KeyValuePair <string, Type>(endpointName, contractType);

            return(this.TakeChannel(cacheAddress, via, out key));
        }
        ChannelFactoryReference CreateChannelFactory(EndpointAddress address)
        {
            KeyValuePair <string, Type> endpointData;

            if (this.endpointMappings.ContainsKey(address))
            {
                endpointData = (KeyValuePair <string, Type>) this.endpointMappings[address];
                if (endpointMappings != null)
                {
                    return(new ChannelFactoryReference(
                               ChannelManagerHelpers.CreateChannelFactory(endpointData.Key, endpointData.Value, this.endpoints),
                               endpointData.Value));
                }
            }

            return(null);
        }
        public PooledChannel TakeChannel(EndpointAddress address, Uri via, out ChannelPoolKey key)
        {
            PooledChannel channel = null;

            key = null;

            if (!this.closed)
            {
                ChannelFactoryReference factory = this.TakeChannelFactory(address);
                if (factory != null)
                {
                    bool channelCreated = false;
                    try
                    {
                        EndpointAddress cacheAddress = address;
                        if (factory.SupportsClientAuthentication)
                        {
                            cacheAddress = ChannelManagerHelpers.BuildCacheAddressWithIdentity(address);
                        }

                        channel = this.channelPool.TakeConnection(cacheAddress, via, ServiceDefaults.OpenTimeout, out key);
                        while (channel != null && channel.State != CommunicationState.Opened)
                        {
                            // Loop will exit because non-opened channels are returned with 'connectionStillGood=false'
                            this.channelPool.ReturnConnection(key, channel, false, ServiceDefaults.CloseTimeout);
                            channel = this.channelPool.TakeConnection(cacheAddress, via, ServiceDefaults.OpenTimeout, out key);
                        }

                        if (channel == null)
                        {
                            channel = this.CreateChannel(factory, address, via, out channelCreated);
                        }
                    }
                    finally
                    {
                        if (!channelCreated)
                        {
                            this.ReturnChannelFactory(address, factory);
                        }
                    }
                }
            }

            return(channel);
        }
        PooledChannel CreateChannel(ChannelFactoryReference factory, EndpointAddress address, Uri via, out bool channelCreated)
        {
            PooledChannel pooledChannel = null;

            channelCreated = false;

            IChannel channel = ChannelManagerHelpers.CreateChannel(factory.ContractType, factory.ChannelFactory, (via == defaultViaUri) ? null : via.ToString());

            if (channel != null)
            {
                pooledChannel = new PooledChannel(this, factory, address, channel);
                lock (this.ThisLock)
                {
                    this.newChannels.Add(pooledChannel);
                }

                channelCreated = true;
            }

            return(pooledChannel);
        }
 public void Close(TimeSpan timeout)
 {
     ChannelManagerHelpers.CloseCommunicationObject(this.channelFactory, timeout);
 }
 public void Abort()
 {
     ChannelManagerHelpers.CloseCommunicationObject(this.channelFactory);
 }