Example #1
0
        protected virtual void CloseProxy <I>(IChannelWrapper <I> proxy, bool abort)
        {
            var key = typeof(I).FullName;

            if (abort)
            {
                proxy.Abort();
            }

            if (proxy.CacheCount > 0)
            {
                lock (_lockObj)
                {
                    if (!_proxyCache.ContainsKey(key))
                    {
                        _proxyCache.Add(key, new List <ICachableChannel>());
                    }

                    var cache = _proxyCache[key];
                    if (cache.Where(p => p.CacheKey == key).Count() > 0)
                    {
                        // found in cache
                        var proxyCached = cache.Where(p => p.CacheKey == key).First();
                        proxy.InUse = false;

                        if (abort)
                        {
                            cache.Remove(proxyCached);
                        }
                    }
                    else if (!abort)
                    {
                        // not in cache and not aborted
                        if (proxy.CacheCount > cache.Count)
                        {
                            proxy.InUse = false;
                            cache.Add(proxy);
                        }
                        else
                        {
                            // no room to cache, kill it
                            try
                            {
                                proxy.Close();
                            }
                            catch
                            {
                                proxy.Abort();
                            }
                        }
                    }
                }
            }
            else if (!abort)
            {
                // never cache this proxy
                try
                {
                    proxy.Close();
                }
                catch
                {
                    proxy.Abort();
                }
            }
        }