Ejemplo n.º 1
0
 /// <summary>
 /// Fire the Channel retrieved event when a channel is removed from the pool
 /// </summary>
 /// <param name="cea"></param>
 protected void OnChannelRetrieved(ChannelRetrievedEventArgs cea)
 {
     if (ChannelRetrieved != null)
     {
         ChannelRetrieved(this, cea);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns the Channel Context object which encapsulates the channel itself.
        /// </summary>
        /// <returns></returns>
        public ChannelContext <TChannel> GetChannelContext()
        {
            ChannelContext <TChannel> channCtxt = null;

            if (Monitor.TryEnter(_repopulateLock, Constants.LOCK_ATTEMPT_PERIOD))
            {
                bool channelRetrieved = false;
                try
                {
                    if (_channelList.Count == 0)
                    {
                        RefillPool(false);
                    }

                    while (channCtxt == null)
                    {
                        //chan = _channelList[0];
                        ChannelContext <TChannel> tmpCtxt = _channelList[0];
                        // Double check the lease time.
                        if (!IsChannelExpired(tmpCtxt))
                        {
                            channCtxt        = tmpCtxt;
                            channelRetrieved = true;
                        }

                        // whether expired or not, The channel must be removed from the pool
                        _channelList.RemoveAt(0);
                    }

                    // If all the channels have expired, in this tight loop we may need to refill the pool.
                    if (_channelList.Count == 0)
                    {
                        RefillPool(false);
                    }

                    if (channCtxt == null)
                    {
                        throw new Exception(Constants.EXCEPTION_CANNOT_OBTAIN_CHANNEL_FROM_POOL);
                    }
                }
                finally
                {
                    Monitor.Exit(_repopulateLock);
                    if (channelRetrieved)
                    {
                        ChannelRetrievedEventArgs cea = new ChannelRetrievedEventArgs();
                        cea.RemainingChannelsInPool = _channelList.Count;
                        OnChannelRetrieved(cea);
                    }
                }
                return(channCtxt);
            }
            else
            {
                throw new Exception(Constants.EXCEPTION_CANNOT_OBTAIN_LOCK_GETCHANNEL);
            }
        }