Пример #1
0
 private void Serializer(SerializationProxy.SerializerType serializerType)
 {
     _serializer = new SerializationProxy(serializerType);
 }
Пример #2
0
        /// <summary>
        /// 初始化Provider 提供web.config中sessionState的配置
        /// </summary>
        public override void Initialize(string name, NameValueCollection config)
        {
            Debug.WriteLine("Initialize");
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            if (name.Length == 0)
            {
                name = "MongoSessionStateStore";
            }
            if (string.IsNullOrEmpty(config["description"]))
            {
                config.Remove("description");
                config["description"] = "MongoDb Session State Store provider";
            }

            base.Initialize(name, config);

            _applicationName = System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath;

            Configuration cfg = WebConfigurationManager.OpenWebConfiguration(_applicationName);

            _cfgSection = (SessionStateSection)cfg.GetSection("system.web/sessionState");

            var applicationName = config["applicationName"];

            if (!string.IsNullOrEmpty(applicationName))
            {
                _applicationName = applicationName;
            }

            _connectionStringSettings = ConfigurationManager.ConnectionStrings[config["connectionStringName"]];

            if (_connectionStringSettings == null || string.IsNullOrWhiteSpace(_connectionStringSettings.ConnectionString))
            {
                throw new ProviderException("Connection string cannot be empty");
            }

            _connectionString = _connectionStringSettings.ConnectionString;

            _recordExceptions = !string.IsNullOrEmpty(config["recordExceptions"]) && config["recordExceptions"].ToUpper() == "TRUE";


            bool journal = false;

            if (!string.IsNullOrEmpty(config["journal"]))
            {
                if (config["journal"].ToUpper() == "TRUE")
                {
                    journal = true;
                }
            }

            string       writeConcerStr = config["writeConcernLevel"];
            WriteConcern wc;

            if (!string.IsNullOrEmpty(writeConcerStr))
            {
                writeConcerStr = writeConcerStr.ToUpper();
                switch (writeConcerStr)
                {
                case "W1":
                    wc = new WriteConcern(journal: journal, w: 1);
                    break;

                case "W2":
                    wc = new WriteConcern(journal: journal, w: 2);
                    break;

                case "W3":
                    wc = new WriteConcern(journal: journal, w: 3);
                    break;

                case "WMAJORITY":
                    WriteConcern.WValue aux = "majority";
                    wc = new WriteConcern(aux, journal: journal);
                    break;

                default:
                    throw new InvalidOperationException("unknown writeConcernLevel");
                }
                _databaseSettings = new MongoDatabaseSettings
                {
                    WriteConcern = wc
                };
            }

            string serializeTypeStr = config["SerializationType"];

            SerializationProxy.SerializerType serializerType = SerializationProxy.SerializerType.BsonSerialization;
            if (!string.IsNullOrEmpty(serializeTypeStr))
            {
                serializeTypeStr = serializeTypeStr.ToUpper();
                switch (serializeTypeStr)
                {
                case "raw":
                    serializerType = SerializationProxy.SerializerType.RawSerialization;
                    break;

                case "bson":
                    serializerType = SerializationProxy.SerializerType.BsonSerialization;
                    break;

                default:
                    throw new InvalidOperationException("");
                }
            }
            Serializer(serializerType);
        }