ChannelFactoryRefCache.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / ChannelFactoryRefCache.cs / 1 / ChannelFactoryRefCache.cs

                            //------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------
using System.ServiceModel.Channels;
using System.Collections.Generic; 

namespace System.ServiceModel 
{ 
    sealed class EndpointTrait
        where TChannel : class 
    {
        string endpointConfigurationName;
        EndpointAddress remoteAddress;
        InstanceContext callbackInstance; 

        public EndpointTrait(string endpointConfigurationName, 
            EndpointAddress remoteAddress, 
            InstanceContext callbackInstance)
        { 
            this.endpointConfigurationName = endpointConfigurationName;
            this.remoteAddress = remoteAddress;
            this.callbackInstance = callbackInstance;
        } 

        public override bool Equals(object obj) 
        { 
            EndpointTrait trait1 = obj as EndpointTrait;
            if (trait1 == null) return false; 

            if (!object.ReferenceEquals(this.callbackInstance, trait1.callbackInstance))
                return false;
 
            if (string.CompareOrdinal(this.endpointConfigurationName, trait1.endpointConfigurationName) != 0)
            { 
                return false; 
            }
 
            // EndpointAddress.Equals is used.
            if (this.remoteAddress != trait1.remoteAddress)
                return false;
 
            return true;
        } 
 
        public override int GetHashCode()
        { 
            int hashCode = 0;
            if (this.callbackInstance != null)
            {
                hashCode ^= this.callbackInstance.GetHashCode(); 
            }
 
            hashCode ^= this.endpointConfigurationName.GetHashCode(); 
            if (this.remoteAddress != null)
                hashCode ^= this.remoteAddress.GetHashCode(); 

            return hashCode;
        }
 
        public ChannelFactory CreateChannelFactory()
        { 
            if (this.callbackInstance != null) 
                return CreateDuplexFactory();
 
            return CreateSimplexFactory();
        }

        DuplexChannelFactory CreateDuplexFactory() 
        {
            if (this.remoteAddress != null) 
            { 
                return new DuplexChannelFactory(this.callbackInstance, this.endpointConfigurationName, this.remoteAddress);
            } 

            return new DuplexChannelFactory(this.callbackInstance, this.endpointConfigurationName);
        }
 
        ChannelFactory CreateSimplexFactory()
        { 
            if (this.remoteAddress != null) 
            {
                return new ChannelFactory(this.endpointConfigurationName, this.remoteAddress); 
            }

            return new ChannelFactory(this.endpointConfigurationName);
        } 
    }
 
    sealed class ChannelFactoryRef 
        where TChannel : class
    { 
        ChannelFactory channelFactory;
        int refCount = 1;

        public ChannelFactoryRef(ChannelFactory channelFactory) 
        {
            this.channelFactory = channelFactory; 
        } 

        public void AddRef() 
        {
            this.refCount++;
        }
 
        // The caller should call Close/Abort when the return value is true.
        public bool Release() 
        { 
            --this.refCount;
            DiagnosticUtility.DebugAssert(this.refCount >= 0, "RefCount should not be less than zero."); 

            if (this.refCount == 0)
            {
                return true; 
            }
 
            return false; 
        }
 
        public void Close(TimeSpan timeout)
        {
            this.channelFactory.Close(timeout);
        } 

        public void Abort() 
        { 
            this.channelFactory.Abort();
        } 

        public ChannelFactory ChannelFactory
        {
            get 
            {
                return this.channelFactory; 
            } 
        }
    } 

    class ChannelFactoryRefCache : MruCache, ChannelFactoryRef>
        where TChannel : class
    { 
        static EndpointTraitComparer DefaultEndpointTraitComparer = new EndpointTraitComparer();
        class EndpointTraitComparer : IEqualityComparer> 
        { 
            public bool Equals(EndpointTrait x, EndpointTrait y)
            { 
                if (x != null)
                {
                    if (y != null)
                        return x.Equals(y); 

                    return false; 
                } 

                if (y != null) 
                    return false;

                return true;
            } 

            public int GetHashCode(EndpointTrait obj) 
            { 
                if (obj == null)
                    return 0; 

                return obj.GetHashCode();
            }
        } 

        public ChannelFactoryRefCache(int watermark) 
            : base(watermark * 4 / 5, watermark, DefaultEndpointTraitComparer) 
        { }
 
        protected override void OnSingleItemRemoved(ChannelFactoryRef item)
        {
            // Remove from cache.
            if (item.Release()) 
            {
                item.Abort(); 
            } 
        }
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK