protected override void OnItemAgedOutOfCache(ChannelFactoryRef <TChannel> item)
 {
     if (TD.ClientBaseChannelFactoryAgedOutofCacheIsEnabled())
     {
         TD.ClientBaseChannelFactoryAgedOutofCache(this.watermark, this);
     }
 }
Example #2
0
 protected override void OnSingleItemRemoved(ChannelFactoryRef <TChannel> item)
 {
     if (item.Release())
     {
         item.Abort();
     }
 }
Example #3
0
        protected ClientBase(Binding binding, EndpointAddress remoteAddress)
        {
            if (binding == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(binding));
            }

            if (remoteAddress == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(remoteAddress));
            }

            MakeCacheSettingReadOnly();

            if (s_cacheSetting == CacheSetting.AlwaysOn)
            {
                _endpointTrait = new ProgrammaticEndpointTrait <TChannel>(binding, remoteAddress, null);
                InitializeChannelFactoryRef();
            }
            else
            {
                _channelFactoryRef = new ChannelFactoryRef <TChannel>(new ChannelFactory <TChannel>(binding, remoteAddress));
                _channelFactoryRef.ChannelFactory.TraceOpenAndClose = false;
                TryDisableSharing();
            }
        }
Example #4
0
        void InitializeChannelFactoryRef()
        {
            Fx.Assert(_channelFactoryRef == null, "The channelFactory should have never been assigned");
            Fx.Assert(_canShareFactory, "GetChannelFactoryFromCache can be called only when canShareFactory is true");
            lock (s_staticLock)
            {
                ChannelFactoryRef <TChannel> factoryRef;
                if (s_factoryRefCache.TryGetValue(_endpointTrait, out factoryRef))
                {
                    if (factoryRef.ChannelFactory.State != CommunicationState.Opened)
                    {
                        // Remove the bad ChannelFactory.
                        s_factoryRefCache.Remove(_endpointTrait);
                    }
                    else
                    {
                        _channelFactoryRef = factoryRef;
                        _channelFactoryRef.AddRef();
                        _useCachedFactory = true;
                        if (WcfEventSource.Instance.ClientBaseChannelFactoryCacheHitIsEnabled())
                        {
                            WcfEventSource.Instance.ClientBaseChannelFactoryCacheHit(this);
                        }
                        return;
                    }
                }
            }

            if (_channelFactoryRef == null)
            {
                // Creating the ChannelFactory at initial time to catch configuration exception earlier.
                _channelFactoryRef = CreateChannelFactoryRef(_endpointTrait);
            }
        }
        private void TryDisableSharing()
        {
            if (!this.sharingFinalized)
            {
                lock (this.finalizeLock)
                {
                    if (!this.sharingFinalized)
                    {
                        this.canShareFactory  = false;
                        this.sharingFinalized = true;
                        if (this.useCachedFactory)
                        {
                            ChannelFactoryRef <TChannel> channelFactoryRef = this.channelFactoryRef;
                            this.channelFactoryRef = ClientBase <TChannel> .CreateChannelFactoryRef(this.endpointTrait);

                            this.useCachedFactory = false;
                            lock (ClientBase <TChannel> .staticLock)
                            {
                                if (!channelFactoryRef.Release())
                                {
                                    channelFactoryRef = null;
                                }
                            }
                            if (channelFactoryRef != null)
                            {
                                channelFactoryRef.Abort();
                            }
                        }
                    }
                }
            }
        }
 private void InitializeChannelFactoryRef()
 {
     lock (ClientBase <TChannel> .staticLock)
     {
         ChannelFactoryRef <TChannel> ref2;
         if (ClientBase <TChannel> .factoryRefCache.TryGetValue(this.endpointTrait, out ref2))
         {
             if (ref2.ChannelFactory.State != CommunicationState.Opened)
             {
                 ClientBase <TChannel> .factoryRefCache.Remove(this.endpointTrait);
             }
             else
             {
                 this.channelFactoryRef = ref2;
                 this.channelFactoryRef.AddRef();
                 this.useCachedFactory = true;
                 return;
             }
         }
     }
     if (this.channelFactoryRef == null)
     {
         this.channelFactoryRef = ClientBase <TChannel> .CreateChannelFactoryRef(this.endpointTrait);
     }
 }
Example #7
0
 protected override void OnItemAgedOutOfCache(ChannelFactoryRef <TChannel> item)
 {
     if (WcfEventSource.Instance.ClientBaseChannelFactoryAgedOutofCacheIsEnabled())
     {
         WcfEventSource.Instance.ClientBaseChannelFactoryAgedOutofCache(_watermark, this);
     }
 }
        protected override void OnSingleItemRemoved(ChannelFactoryRef <TChannel> item)
        {
            // Remove from cache.
            if (item.Release())
            {
                item.Abort();
            }

            if (TD.ClientBaseCachedChannelFactoryCountIsEnabled())
            {
                TD.ClientBaseCachedChannelFactoryCount(this.Count, this.watermark, this);
            }
        }
 protected ClientBase(ServiceEndpoint endpoint)
 {
     this.canShareFactory = true;
     this.syncRoot        = new object();
     this.finalizeLock    = new object();
     if (endpoint == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("endpoint");
     }
     this.channelFactoryRef = new ChannelFactoryRef <TChannel>(new ChannelFactory <TChannel>(endpoint));
     this.channelFactoryRef.ChannelFactory.TraceOpenAndClose = false;
     this.TryDisableSharing();
 }
Example #10
0
        protected override void OnSingleItemRemoved(ChannelFactoryRef <TChannel> item)
        {
            // Remove from cache.
            if (item.Release())
            {
                item.Abort();
            }

            if (WcfEventSource.Instance.ClientBaseCachedChannelFactoryCountIsEnabled())
            {
                WcfEventSource.Instance.ClientBaseCachedChannelFactoryCount(Count, _watermark, this);
            }
        }
Example #11
0
        // Once the channel is created, we can't disable caching.
        // This method can be called safely multiple times.
        // this.sharingFinalized is set the first time the method is called.
        // Subsequent calls are essentially no-ops.
        void TryDisableSharing()
        {
            if (_sharingFinalized)
            {
                return;
            }

            lock (finalizeLock)
            {
                if (_sharingFinalized)
                {
                    return;
                }

                _canShareFactory  = false;
                _sharingFinalized = true;

                if (_useCachedFactory)
                {
                    ChannelFactoryRef <TChannel> pendingFactoryRef = _channelFactoryRef;
                    _channelFactoryRef = CreateChannelFactoryRef(_endpointTrait);
                    _useCachedFactory  = false;

                    lock (s_staticLock)
                    {
                        if (!pendingFactoryRef.Release())
                        {
                            pendingFactoryRef = null;
                        }
                    }

                    if (pendingFactoryRef != null)
                    {
                        pendingFactoryRef.Abort();
                    }
                }
            }

            // can be done outside the lock since the lines below do not access shared data.
            // also the use of this.sharingFinalized in the lines above ensures that tracing
            // happens only once and only when needed.
            if (WcfEventSource.Instance.ClientBaseUsingLocalChannelFactoryIsEnabled())
            {
                WcfEventSource.Instance.ClientBaseUsingLocalChannelFactory(this);
            }
        }
 protected ClientBase(Binding binding, EndpointAddress remoteAddress)
 {
     this.canShareFactory = true;
     this.syncRoot        = new object();
     this.finalizeLock    = new object();
     if (binding == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("binding");
     }
     if (remoteAddress == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("remoteAddress");
     }
     this.channelFactoryRef = new ChannelFactoryRef <TChannel>(new ChannelFactory <TChannel>(binding, remoteAddress));
     this.channelFactoryRef.ChannelFactory.TraceOpenAndClose = false;
     this.TryDisableSharing();
 }
Example #13
0
        protected ClientBase(ServiceEndpoint endpoint)
        {
            if (endpoint == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(endpoint));
            }

            MakeCacheSettingReadOnly();

            if (s_cacheSetting == CacheSetting.AlwaysOn)
            {
                _endpointTrait = new ServiceEndpointTrait <TChannel>(endpoint, null);
                InitializeChannelFactoryRef();
            }
            else
            {
                _channelFactoryRef = new ChannelFactoryRef <TChannel>(new ChannelFactory <TChannel>(endpoint));
                _channelFactoryRef.ChannelFactory.TraceOpenAndClose = false;
                TryDisableSharing();
            }
        }