Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a module and prepares it to handle requests.
        /// </summary>
        /// <param name="application">An <see cref="System.Web.HttpApplication"/> object that provides
        /// references to the intrinsic server objects (for example,
        /// Request, Response, Session, and Server) used to service HTTP requests.
        /// </param>
        /// <remarks>
        /// </remarks>
        void IHttpModule.Init(HttpApplication application)
        {
            if (application == null)
            {
                throw new ArgumentNullException("application");
            }
            HttpContext context = application.Context;

            this._onAcqStateHandler     = new EventHandler(OnAcquireRequestState);
            this._onReleaseStateHandler = new EventHandler(OnReleaseRequestState);
            this._onDisposeHandler      = new EventHandler(this.OnDisposed);
            this._onEnd = new EventHandler(this.OnEnd);

            application.AcquireRequestState += this._onAcqStateHandler;
            application.ReleaseRequestState += this._onReleaseStateHandler;
            application.Disposed            += this._onDisposeHandler;
            application.EndRequest          += this._onEnd;

            if (NCacheSessionStateConfigReader.LoadSessionLocationSettings() != null)
            {
                this._isLocationAffinityEnabled = true;
            }

            GetCache(context);
            application.Application["NSessionStateModule"] = this;
        }
Ejemplo n.º 2
0
        private void InitializeCache()
        {
            lock (s_dataLock)
            {
                try
                {
                    if (_cache == null)
                    {
                        LegacyProvider.s_onAppDomainUnload = new EventHandler(OnAppDomainUnload);
                        System.Threading.Thread.GetDomain().DomainUnload += LegacyProvider.s_onAppDomainUnload;

                        if (_logs || _detailedLogs)
                        {
                            if (_ncacheLog == null)
                            {
                                _ncacheLog = new NCacheLogger();
                                _ncacheLog.Initialize(LoggerNames.SessionStoreProvider, _cacheId);

                                if (_detailedLogs)
                                {
                                    NCacheLog.SetLevel("all");
                                }
                                else
                                {
                                    if (_logs)
                                    {
                                        NCacheLog.SetLevel("info");
                                    }
                                }
                            }
                        }
                        if (_isLocationAffinityEnabled)
                        {
                            _cache = new RegionalCache(_ncacheLog, NCacheSessionStateConfigReader.LoadSessionLocationSettings());
                        }
                        else
                        {
                            _cache = new SingleRegionCache(_operationRetry, _operationRetryInterval);
                        }

                        _cache.InitializeCache(_cacheId);
                        _cache.ExceptionsEnabled = true;
                        s_cacheNeedInit          = false;
                        if (_logs)
                        {
                            NCacheLog.Info("NSessionStoreProvider initialized");
                        }
                        Thread.Sleep(_inprocDelay);
                    }
                }
                catch (Exception exc)
                {
                    _cache = null; // so that next time cache can be initialized. Check the above condition if(_cache == null)
                    RaiseException(exc);
                }
            }
        }
Ejemplo n.º 3
0
        public override void Initialize(string name, NameValueCollection config)
        {
            base.Initialize(name, config);
            _store = new SessionStore();
            var                 appName       = System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath;
            Configuration       cfg           = WebConfigurationManager.OpenWebConfiguration(appName);
            SessionStateSection sessionConfig = (SessionStateSection)cfg.GetSection("system.web/sessionState");

            config["defaultSessionTimeout"] = sessionConfig.Timeout.Minutes.ToString();
            _store.Initialize(name, config, NCacheSessionStateConfigReader.LoadSessionLocationSettings());
        }
Ejemplo n.º 4
0
        private void InitializeCache(HttpApplication application)
        {
            this._cacheId = ConfigurationSettings.AppSettings["cacheName"];
            if (this._cacheId == null || this._cacheId == string.Empty)
            {
                throw new ConfigurationException("The 'cacheName' attribute cannot be null or empty string");
            }

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

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

                        case 1: this._writeExceptionsToEventLog = value; break;

                        case 2: this._logs = value; break;

                        case 3: this._detailedLogs = value; break;

                        case 4: this._clearASPSession = value; break;
                        }
                    }
                }
            }
            if (this._logs || this._detailedLogs)
            {
                if (_ncacheLog == null)
                {
                    _ncacheLog = new NCacheLogger();
                    _ncacheLog.Initialize(LoggerNames.HttpModule, this._cacheId);

                    if (this._detailedLogs)
                    {
                        NCacheLog.SetLevel("all");
                    }
                    else
                    {
                        if (this._logs)
                        {
                            NCacheLog.SetLevel("info");
                        }
                    }
                }
            }

            _applicationId = ConfigurationSettings.AppSettings["sessionAppId"];

            if (_isLocationAffinityEnabled)
            {
                _cache = new RegionalCache(_ncacheLog, NCacheSessionStateConfigReader.LoadSessionLocationSettings());
            }
            else
            {
                _cache = new SingleRegionCache();
            }

            try
            {
                this._cache.InitializeCache(this._cacheId);
                this._cache.ExceptionsEnabled = true;
                if (NCacheLog != null)
                {
                    NCacheLog.Info("NSessionStateModule initialized");
                }
                application.Application["NSessionStateModule.Cache"] = this._cache;
            }
            catch (Exception e)
            {
                RaiseExceptions(e);
            }
        }
Ejemplo n.º 5
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 override void Initialize(string name, NameValueCollection config)
        {
            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;
            }

            //initialize the base class
            base.Initialize(name, config);

            //get the application virtual path
            _appName = System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath;

            if (NCacheSessionStateConfigReader.LoadSessionLocationSettings() != 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 != "true" && configVal != "false")
                    {
                        throw new ConfigurationErrorsException("The '" + boolValStrings[i] + "' attribute must be one of the following values: true, false.");
                    }
                    else
                    {
                        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;
                            _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"]);
            }

            //get cache name from configurations
            _cacheId = config["cacheName"];


            Configuration       cfg           = WebConfigurationManager.OpenWebConfiguration(_appName);
            SessionStateSection sessionConfig = (SessionStateSection)cfg.GetSection("system.web/sessionState");

            _defaultTimeout = sessionConfig.Timeout.Minutes;

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