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>(WcfBinding.GetBinding(bindingType), new EndpointAddress(remoteUri));

                        ChannelFactoryDictionary.Add(key, result);

                        return(result.CreateChannel());
                    }
                }
            }
            else
            {
                return(new ChannelFactory <TChannel>(WcfBinding.GetBinding(bindingType), new EndpointAddress(remoteUri)).CreateChannel());
            }
        }
Example #2
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] = WcfBinding.GetBinding(bindingType);
            paramValues[1] = new EndpointAddress(remoteUri);

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