Example #1
0
        public static TChannel CreateChannel(Type bindingType, string remoteUri, bool fromCaching = true)
        {
            if (fromCaching)
            {
                string key = string.Format(ChannelFactoryDictionaryKeyStringFormat, bindingType.FullName, string.IsNullOrEmpty(remoteUri) ? string.Empty : remoteUri.ToLowerInvariant());

                if (ChannelFactoryDictionary.ContainsKey(key))
                {
                    return(ChannelFactoryDictionary[key].CreateChannel());
                }

                lock (SyncRoot)
                {
                    if (ChannelFactoryDictionary.ContainsKey(key))
                    {
                        return(ChannelFactoryDictionary[key].CreateChannel());
                    }
                    else
                    {
                        ChannelFactory <TChannel> result = new ChannelFactory <TChannel>(WcfServiceType.GetBinding(bindingType), new EndpointAddress(remoteUri));

                        ChannelFactoryDictionary.Add(key, result);

                        return(result.CreateChannel());
                    }
                }
            }
            else
            {
                return(new ChannelFactory <TChannel>(WcfServiceType.GetBinding(bindingType), new EndpointAddress(remoteUri)).CreateChannel());
            }
        }
Example #2
0
        /// <summary>
        /// Get Wcf client instance.
        /// </summary>
        /// <typeparam name="TTypeBuilder">The proxy class builder.</typeparam>
        /// <param name="caching">Caching dictionary to use.</param>
        /// <param name="bindingType">The type of <see cref="T:System.ServiceModel.Channels.Binding" /> for the service.</param>
        /// <param name="remoteUri">The URI that identifies the service endpoint.</param>
        /// <param name="fromCaching">Whether get instance from caching or not.</param>
        /// <returns>Instance of a ClientBase derived class.</returns>
        private static TChannel GetInstance <TTypeBuilder>(Dictionary <string, TChannel> caching, Type bindingType, string remoteUri, bool fromCaching = true) where TTypeBuilder : IWcfClientTypeBuilder, new()
        {
            if (fromCaching)
            {
                string key = string.Format(WcfClientProxyDictionaryKeyStringFormat, bindingType.GetHashCode(), string.IsNullOrEmpty(remoteUri) ? string.Empty : remoteUri.ToLowerInvariant());

                Lock.AcquireReaderLock(Timeout.Infinite);

                try
                {
                    if (caching.ContainsKey(key))
                    {
                        return(caching[key]);
                    }
                    else
                    {
                        LockCookie lockCookie = Lock.UpgradeToWriterLock(Timeout.Infinite);

                        try
                        {
                            if (caching.ContainsKey(key))
                            {
                                return(caching[key]);
                            }
                            else
                            {
                                Type type = WcfClientType.BuildType <TChannel, TTypeBuilder>();

                                TChannel result = (TChannel)Activator.CreateInstance(type, WcfServiceType.GetBinding(bindingType), new EndpointAddress(remoteUri));

                                caching.Add(key, result);

                                return(result);
                            }
                        }
                        finally
                        {
                            Lock.DowngradeFromWriterLock(ref lockCookie);
                        }
                    }
                }
                finally
                {
                    Lock.ReleaseReaderLock();
                }
            }
            else
            {
                Type type = WcfClientType.BuildType <TChannel, TTypeBuilder>();

                return((TChannel)Activator.CreateInstance(type, WcfServiceType.GetBinding(bindingType), new EndpointAddress(remoteUri)));
            }
        }
Example #3
0
        public static TChannel CreateChannel(Type bindingType, string remoteUri, bool fromCaching = true)
        {
            if (fromCaching)
            {
                string key = string.Format(ChannelFactoryDictionaryKeyStringFormat, bindingType.GetHashCode(), string.IsNullOrEmpty(remoteUri) ? string.Empty : remoteUri.ToLowerInvariant());

                Lock.AcquireReaderLock(Timeout.Infinite);

                try
                {
                    if (ChannelFactoryDictionary.ContainsKey(key))
                    {
                        return(ChannelFactoryDictionary[key].CreateChannel());
                    }
                    else
                    {
                        LockCookie lockCookie = Lock.UpgradeToWriterLock(Timeout.Infinite);

                        try
                        {
                            if (ChannelFactoryDictionary.ContainsKey(key))
                            {
                                return(ChannelFactoryDictionary[key].CreateChannel());
                            }
                            else
                            {
                                ChannelFactory <TChannel> result = new ChannelFactory <TChannel>(WcfServiceType.GetBinding(bindingType), new EndpointAddress(remoteUri));

                                ChannelFactoryDictionary.Add(key, result);

                                return(result.CreateChannel());
                            }
                        }
                        finally
                        {
                            Lock.DowngradeFromWriterLock(ref lockCookie);
                        }
                    }
                }
                finally
                {
                    Lock.ReleaseReaderLock();
                }
            }
            else
            {
                return(new ChannelFactory <TChannel>(WcfServiceType.GetBinding(bindingType), new EndpointAddress(remoteUri)).CreateChannel());
            }
        }
Example #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DynamicClientProxyBase" /> class.
        /// </summary>
        /// <param name="proxyType">Type of the proxy.</param>
        /// <param name="bindingType">Type of the binding.</param>
        /// <param name="remoteUri">The remote URI.</param>
        public DynamicClientProxyBase(Type proxyType, Type bindingType, string remoteUri)
        {
            ServicePointManager.DefaultConnectionLimit = int.MaxValue;

            this.ProxyType = proxyType;

            Type[] paramTypes = new Type[2];
            paramTypes[0] = typeof(Binding);
            paramTypes[1] = typeof(EndpointAddress);

            object[] paramValues = new object[2];
            paramValues[0] = WcfServiceType.GetBinding(bindingType);
            paramValues[1] = new EndpointAddress(remoteUri);

            this.ParamTypes  = paramTypes;
            this.ParamValues = paramValues;
        }