/// <summary>
        ///     获取部分配置信息
        /// </summary>
        /// <param name="configKey">配置文件KEY名称</param>
        /// <returns>如果不存在指定条件的配置文件,则返回null</returns>
        /// <exception cref="ArgumentNullException">参数不能为空</exception>
        public string GetPartialConfig(string configKey)
        {
            if (string.IsNullOrEmpty(configKey))
            {
                throw new ArgumentNullException("configKey");
            }
            #region Get cache first.

            string config = null;
            //has cache.
            if (_configCaches.TryGetValue(configKey, out config))
            {
                return(config);
            }

            #endregion
            PrepareConnection();
            #region Get remote config.

            CSNGetPartialConfigRequestMessage requestMessage = new CSNGetPartialConfigRequestMessage();
            requestMessage.Key = configKey;
            BusinessMessageTransaction transaction = _transactionManager.Create(IdentityHelper.Create((IPEndPoint)_channel.LocalEndPoint), _channel);
            if (transaction == null)
            {
                throw new Exception("Cannot create a CSN message transaction!");
            }
            AutoResetEvent autoResetEvent = new AutoResetEvent(false);
            transaction.ResponseArrived += delegate(object sender, LightSingleArgEventArgs <BaseMessage> e)
            {
                CSNGetPartialConfigResponseMessage rspMsg = (CSNGetPartialConfigResponseMessage)e.Target;
                if (rspMsg.ErrorId == 0 && !string.IsNullOrEmpty(rspMsg.Config))
                {
                    //add cache.
                    _configCaches[configKey] = rspMsg.Config;
                    config = rspMsg.Config;
                }
                autoResetEvent.Set();
            };
            transaction.Failed += delegate
            {
                _transactionManager.Remove(transaction.Identity);
                autoResetEvent.Set();
            };
            transaction.Timeout += delegate
            {
                _transactionManager.Remove(transaction.Identity);
                autoResetEvent.Set();
            };
            transaction.SendRequest(requestMessage);
            //wait 35s.
            autoResetEvent.WaitOne(35000);
            autoResetEvent.Dispose();
            return(config);

            #endregion
        }
        /// <summary>
        ///     获取指定表的数据
        /// </summary>
        /// <param name="database">数据库名</param>
        /// <param name="table">表名</param>
        /// <param name="hasCache">缓存标识</param>
        /// <returns>返回表数据</returns>
        /// <exception cref="ArgumentNullException">参数不能为空</exception>
        public DataTable GetTable(string database, string table, bool hasCache)
        {
            DataTable result    = null;
            Exception exception = null;

            if (hasCache)
            {
                //get cache by default.
                if ((result = GetCacheTable(string.Format("{0}.{1}", database, table))) != null)
                {
                    return(result);
                }
            }
            PrepareConnection();
            CSNGetDataTableRequestMessage requestMessage = new CSNGetDataTableRequestMessage();

            requestMessage.DatabaseName = database;
            requestMessage.TableName    = table;
            BusinessMessageTransaction transaction = _transactionManager.Create(IdentityHelper.Create((IPEndPoint)_channel.LocalEndPoint), _channel);

            if (transaction == null)
            {
                throw new Exception("Cannot create a CSN message transaction!");
            }
            AutoResetEvent autoResetEvent = new AutoResetEvent(false);

            transaction.ResponseArrived += delegate(object sender, LightSingleArgEventArgs <BaseMessage> e)
            {
                CSNGetDataTableResponseMessage responseMessage = (CSNGetDataTableResponseMessage)e.Target;
                result = responseMessage.Tables == null ? null : responseMessage.Tables;
                autoResetEvent.Set();
            };
            transaction.Failed += delegate
            {
                _transactionManager.Remove(transaction.Identity);
                exception = new Exception("Failed! Cannot get table data config from CSN service, #table: " + table);
                autoResetEvent.Set();
            };
            transaction.Timeout += delegate
            {
                _transactionManager.Remove(transaction.Identity);
                exception = new Exception("Timeout! Cannot get table data config from CSN service, #table: " + table);
                autoResetEvent.Set();
            };
            transaction.SendRequest(requestMessage);
            //wait 35s.
            autoResetEvent.WaitOne(35000);
            autoResetEvent.Dispose();
            if (exception != null)
            {
                throw exception;
            }
            #region Add table cahce.

            if (hasCache && result != null)
            {
                lock (_lockTablesObj)
                    _cacheTables.Add(string.Format("{0}.{1}", database, table), result);
            }

            #endregion
            return(result);
        }
        /// <summary>
        ///     获取某个字段的值
        /// </summary>
        /// <typeparam name="T">字段类型</typeparam>
        /// <param name="database">数据库名</param>
        /// <param name="table">表名</param>
        /// <param name="service">服务名称</param>
        /// <param name="field">字段名</param>
        /// <returns>返回相应字段的值</returns>
        /// <exception cref="ArgumentNullException">参数不能为空</exception>
        public string GetField(string database, string table, string service, string field)
        {
            string result = null;

            #region Get cache first.

            Dictionary <string, string> items;
            //has cache.
            if (_cacheKeys.TryGetValue(service, out items))
            {
                return(items.TryGetValue(field, out result) ? result : null);
            }

            #endregion
            PrepareConnection();
            #region Get remote config.

            Exception exception = null;
            CSNGetKeyDataRequestMessage requestMessage = new CSNGetKeyDataRequestMessage();
            requestMessage.DatabaseName = database;
            requestMessage.TableName    = table;
            requestMessage.ServiceName  = service;

            BusinessMessageTransaction transaction = _transactionManager.Create(IdentityHelper.Create((IPEndPoint)_channel.LocalEndPoint), _channel);
            if (transaction == null)
            {
                throw new Exception("Cannot create a CSN message transaction!");
            }
            AutoResetEvent autoResetEvent = new AutoResetEvent(false);
            transaction.ResponseArrived += delegate(object sender, LightSingleArgEventArgs <BaseMessage> e)
            {
                CSNGetKeyDataResponseMessage rspMessage = (CSNGetKeyDataResponseMessage)e.Target;
                #region Add Cache.

                Dictionary <string, string> values = new Dictionary <string, string>();
                if (rspMessage.Items != null)
                {
                    foreach (KeyValueItem item in rspMessage.Items)
                    {
                        values.Add(item.Key, item.Value);
                    }
                    _cacheKeys.Add(service, values);
                }
                //get result.
                result = values.ContainsKey(field) ? values[field] : null;

                #endregion
                autoResetEvent.Set();
            };
            transaction.Failed += delegate
            {
                _transactionManager.Remove(transaction.Identity);
                exception = new Exception("Failed! Cannot get key data config from CSN service, #key: " + field);
                autoResetEvent.Set();
            };
            transaction.Timeout += delegate
            {
                _transactionManager.Remove(transaction.Identity);
                exception = new Exception("Timeout! Cannot get key data config from CSN service, #key: " + field);
                autoResetEvent.Set();
            };
            transaction.SendRequest(requestMessage);
            //wait 35s.
            autoResetEvent.WaitOne(35000);
            autoResetEvent.Dispose();
            if (exception != null)
            {
                throw exception;
            }

            #endregion

            return(result);
        }