示例#1
0
 public RegionalKeyWrapper(NCacheSessionStateSettings affinitySettings, ISessionKeyManager keyManager)
 {
     _affinitySettings = affinitySettings;
     _keyManager       = keyManager;
     foreach (DictionaryEntry entry in _affinitySettings.PrimaryCache)
     {
         _primaryPrefix = (string)entry.Key;
     }
 }
示例#2
0
        public NCacheSessionStoreService(IOptions <NCacheSessionConfiguration> options, ISessionKeyManager keyManager)
        {
            _options    = options;
            _keyManager = keyManager;
            NameValueCollection nvc = new NameValueCollection {
                { "cacheName", _options.Value.CacheName }
            };

            if (_options.Value.ExceptionsEnabled != null)
            {
                nvc.Add("exceptionsEnabled", _options.Value.ExceptionsEnabled.Value.ToString());
            }

            if (_options.Value.WriteExceptionsToEventLog != null)
            {
                nvc.Add("writeExceptionsToEventLog", _options.Value.WriteExceptionsToEventLog.Value.ToString());
            }

            if (_options.Value.EnableLogs != null)
            {
                nvc.Add("enableLogs", _options.Value.EnableLogs.Value.ToString());
            }

            if (_options.Value.EnableDetailLogs != null)
            {
                nvc.Add("enableDetailLogs", _options.Value.EnableDetailLogs.Value.ToString());
            }

            if (_options.Value.EnableSessionLocking != null)
            {
                nvc.Add("enableSessionLocking", _options.Value.EnableSessionLocking.Value.ToString());
            }

            if (_options.Value.SessionAppId != null)
            {
                nvc.Add("sessionAppId", _options.Value.SessionAppId);
            }

            if (_options.Value.InprocDelay != null)
            {
                nvc.Add("inprocDelay", _options.Value.InprocDelay.Value.ToString());
            }

            if (_options.Value.OperationRetry != null)
            {
                nvc.Add("operationRetry", _options.Value.OperationRetry.Value.ToString());
            }

            if (_options.Value.OperationRetryInterval != null)
            {
                nvc.Add("operationRetryInterval", _options.Value.OperationRetryInterval.Value.ToString());
            }

            nvc.Add("defaultSessionTimeout", (_options.Value.RequestTimeout != 0? _options.Value.RequestTimeout.ToString():"120"));
            NCacheSessionStateSettings affinitySettings = null;

            if (_options.Value.EnableLocationAffinity != null && _options.Value.EnableLocationAffinity.Value)
            {
                affinitySettings = new NCacheSessionStateSettings
                {
                    PrimaryCache    = new Hashtable(),
                    SecondaryCaches = new Hashtable()
                };
                foreach (var cacheAffinity in _options.Value.AffinityMapping)
                {
                    if (cacheAffinity.CacheName == null || cacheAffinity.CachePrefix == null ||
                        cacheAffinity.CachePrefix.Length < 4)
                    {
                        throw new ConfigurationErrorsException("Invalid cache affinity settings specified. ");
                    }
                    if (affinitySettings.PrimaryCache.Count == 0 &&
                        cacheAffinity.CacheName.Equals(_options.Value.CacheName))
                    {
                        affinitySettings.PrimaryCache.Add(cacheAffinity.CachePrefix, cacheAffinity.CacheName);
                    }
                    else if (!affinitySettings.SecondaryCaches.ContainsKey(cacheAffinity.CachePrefix))
                    {
                        affinitySettings.SecondaryCaches.Add(cacheAffinity.CachePrefix, cacheAffinity.CacheName);
                    }
                }
                if (affinitySettings.PrimaryCache.Count == 0)
                {
                    throw new ConfigurationErrorsException(
                              "No affinity setting has been specified for the primary cache. ");
                }
                _keyManager = new RegionalKeyWrapper(affinitySettings, _keyManager);
            }
            _store = new NCacheCoreSessionStore();
            _store.Initialize(null, nvc, affinitySettings);
        }
示例#3
0
        /// <summary>
        /// Initializes the provider. Takes, as input, the name of the provider and a
        /// NameValueCollection of configuration settings. This method is used to set
        /// property values for the provider instance, including implementation-specific
        /// values and options specified in the configuration file
        /// (Machine.config or Web.config).
        ///
        /// "name" The friendly name of the provider.</param>
        /// config A collection of the name/value pairs representing the
        /// provider-specific attributes specified in the
        /// configuration for this provider.</param>
        /// </summary>
        /// <param name="name">Friendly name of provider</param>
        /// <param name="config">Representing the provider specific attributes</param>
        public virtual void Initialize(string name, NameValueCollection config, NCacheSessionStateSettings regionCacheSettings)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }
            if (string.IsNullOrEmpty(config["cacheName"]))
            {
                throw new ConfigurationErrorsException("The 'cacheName' attribute cannot be null or empty string");
            }


            if (string.IsNullOrEmpty(config["description"]))
            {
                config["description"] = "NCache Session Storage Provider";
            }

            if (name == null || name.Length == 0)
            {
                name = SOURCE;
            }



            _regionalCacheSessionStateSettings = regionCacheSettings;
            if (regionCacheSettings != null)
            {
                _isLocationAffinityEnabled = true;
            }

            string[] boolValStrings = { "exceptionsEnabled", "writeExceptionsToEventLog",
                                        "enableLogs",        "enableDetailLogs", "enableSessionLocking" };
            string   configVal = null;
            bool     value     = false;

            for (int i = 0; i < boolValStrings.Length; i++)
            {
                configVal = config[boolValStrings[i]];
                if (configVal != null)
                {
                    if (!configVal.Equals("true", StringComparison.OrdinalIgnoreCase) &&
                        !configVal.Equals("false", StringComparison.OrdinalIgnoreCase))
                    {
                        throw new ConfigurationErrorsException("The '" + boolValStrings[i] +
                                                               "' attribute must be one of the following values: true, false.");
                    }
                    value = Convert.ToBoolean(configVal);
                    switch (i)
                    {
                    case 0:
                        _exceptionsEnabled = value;
                        break;

                    case 1:
                        _writeExceptionsToEventLog = value;
                        break;

                    case 2:
                        _logs = value;
                        break;

                    case 3:
                    {
                        _detailedLogs = value;
                        if (_detailedLogs)
                        {
                            _logs = value;
                        }
                    }
                    break;

                    case 4:
                        _lockSessions = value;
                        break;
                    }
                }
            }

            if (config["sessionAppId"] != null)
            {
                s_applicationId = config["sessionAppId"];
            }

            if (config["sessionLockingRetry"] != null)
            {
                this._sessionLockingRetries = Convert.ToInt32(config["sessionLockingRetry"]);
            }

            if (config["emptySessionWhenLocked"] != null)
            {
                this._emptySessionWhenLocked = Convert.ToBoolean(config["emptySessionWhenLocked"]);
            }


            _cacheId = config["cacheName"];



            if (config["defaultSessionTimeout"] != null)
            {
                this._defaultTimeout = Convert.ToInt32(config["defaultSessionTimeout"]);
            }

            string inprocDelay = config["inprocDelay"];

            if (!string.IsNullOrEmpty(inprocDelay))
            {
                _inprocDelay = Convert.ToInt32(inprocDelay);
            }

            if (_inprocDelay <= 5000)
            {
                _inprocDelay = 5000;
            }

            if (!String.IsNullOrEmpty(config["operationRetry"]))
            {
                try
                {
                    this._operationRetry = Convert.ToInt32(config["operationRetry"]);
                }
                catch (Exception e)
                {
                    throw new Exception("Invalid value specified for operationRetry.");
                }
            }


            if (!String.IsNullOrEmpty(config["operationRetryInterval"]))
            {
                try
                {
                    this._operationRetryInterval = Convert.ToInt32(config["operationRetryInterval"]);
                }
                catch (Exception e)
                {
                    throw new Exception("Invalid value specified for operationRetryInterval.");
                }
            }

            InitializeCache();
        }