Exemplo n.º 1
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="endpointConfigurationName">The name of the endpoint in the application configuration file.</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, string endpointConfigurationName, string remoteUri, bool fromCaching = true) where TTypeBuilder : IWcfClientTypeBuilder, new()
        {
            if (fromCaching)
            {
                string key = string.Format(WcfClientProxyDictionaryKeyStringFormat, endpointConfigurationName ?? string.Empty, 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 = string.IsNullOrEmpty(remoteUri) ?
                                                  (TChannel)Activator.CreateInstance(type, endpointConfigurationName) :
                                                  (TChannel)Activator.CreateInstance(type, endpointConfigurationName, 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(string.IsNullOrEmpty(remoteUri) ?
                       (TChannel)Activator.CreateInstance(type, endpointConfigurationName) :
                       (TChannel)Activator.CreateInstance(type, endpointConfigurationName, new EndpointAddress(remoteUri)));
            }
        }
Exemplo n.º 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="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, bool fromCaching = true) where TTypeBuilder : IWcfClientTypeBuilder, new()
        {
            if (fromCaching)
            {
                string key = string.Format(WcfClientProxyDictionaryKeyStringFormat, string.Empty, string.Empty);

                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);

                                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));
            }
        }