⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqlstateclientmanager.cs

📁 MasterPage(母版页) 母版页(MasterPage)就相当于模板页
💻 CS
📖 第 1 页 / 共 5 页
字号:
                    staticObjects = HttpStaticObjectsCollection.Deserialize(reader);
                }
                else {
                    staticObjects = SessionStateUtility.GetSessionStaticObjects(context);
                }

                eof = reader.ReadByte();
                if (eof != 0xff) {
                    throw new HttpException(SR.GetString(SR.Invalid_session_state));
                }
            }
            catch (EndOfStreamException) {
                throw new HttpException(SR.GetString(SR.Invalid_session_state));
            }

            return new SessionStateStoreData(sessionItems, staticObjects, timeout);
        }

        private static SessionStateStoreData CreateLegitStoreData(HttpContext context,
                                                    ISessionStateItemCollection sessionItems,
                                                    HttpStaticObjectsCollection staticObjects,
                                                    int timeout) {
            if (sessionItems == null) {
                sessionItems = new SessionStateItemCollection();
            }

            if (staticObjects == null && context != null) {
                staticObjects = SessionStateUtility.GetSessionStaticObjects(context);
            }

            return new SessionStateStoreData(sessionItems, staticObjects, timeout);
        }

        static private void SerializeStoreData(SessionStateStoreData item, int initialStreamSize, out byte[] buf, out int length) {
            MemoryStream s = null;

            try {
                s = new MemoryStream(initialStreamSize);

                Serialize(item, s);
                buf = s.GetBuffer();
                length = (int)s.Length;
            }
            finally {
                if (s != null) {
                    s.Close();
                }
            }
        }

        // This method will take an item and serialize it
        private static void Serialize(SessionStateStoreData item, Stream stream) {
            bool hasItems = true;
            bool hasStaticObjects = true;

            BinaryWriter writer = new BinaryWriter(stream);
            writer.Write(item.Timeout);

            if (item.Items == null || item.Items.Count == 0) {
                hasItems = false;
            }
            writer.Write(hasItems);

            if (item.StaticObjects == null || item.StaticObjects.NeverAccessed) {
                hasStaticObjects = false;
            }
            writer.Write(hasStaticObjects);

            if (hasItems) {
                ((SessionStateItemCollection)item.Items).Serialize(writer);
            }

            if (hasStaticObjects) {
                item.StaticObjects.Serialize(writer);
            }

            // Prevent truncation of the stream
            writer.Write(unchecked((byte)0xff));
        }



        public override void ReleaseItemExclusive(HttpContext context, 
                                String id, 
                                object lockId) {
            //Debug.Trace("SqlSessionStateStore", "Calling Sql ReleaseExclusive, id=" + id);
            //Debug.Assert(lockId != null, "lockId != null");
            //Debug.Assert(context != null, "context != null");

            bool                usePooling = true;
            SqlStateConnection  conn = null;
            int                 lockCookie = (int)lockId;

            try {
                //SessionIDManager.CheckIdLength(id, true /* throwOnFail */);

                conn = GetConnection(id, ref usePooling);
                try {
                    SqlCommand cmd = conn.TempReleaseExclusive;
                    
                    cmd.Parameters[0].Value = id + _partitionInfo.AppSuffix;
                    cmd.Parameters[1].Value = lockCookie;
                    cmd.ExecuteNonQuery();
                }
                catch (Exception e) {
                    ThrowSqlConnectionException(conn.Connection, e);
                }

            }
            finally {
                DisposeOrReuseConnection(ref conn, usePooling);
            }
        }

        public override void SetAndReleaseItemExclusive(HttpContext context, 
                                    String id, 
                                    SessionStateStoreData item, 
                                    object lockId, 
                                    bool newItem) {
            byte []             buf;
            int                 length;
            SqlCommand          cmd;
            bool                usePooling = true;
            SqlStateConnection  conn = null;
            int                 lockCookie;

            //Debug.Assert(context != null, "context != null");

            try {
                //Debug.Trace("SqlSessionStateStore", "Calling Sql Set, id=" + id);

                //Debug.Assert(item.Items != null, "item.Items != null");
                //Debug.Assert(item.StaticObjects != null, "item.StaticObjects != null");

                //SessionIDManager.CheckIdLength(id, true /* throwOnFail */);

                try {
                    SerializeStoreData(item, ITEM_SHORT_LENGTH, out buf, out length);
                }
                catch {
                    if (!newItem) {
                        ((SessionStateStoreProviderBase)this).ReleaseItemExclusive(context, id, lockId);
                    }
                    throw;
                }

                // Save it to the store

                if (lockId == null) {
                    lockCookie = 0;
                }
                else {
                    lockCookie = (int)lockId;
                }

                conn = GetConnection(id, ref usePooling);
                
                if (!newItem) {
                    //Debug.Assert(_rqOrigStreamLen > 0, "_rqOrigStreamLen > 0");
                    if (length <= ITEM_SHORT_LENGTH) {
                        if (_rqOrigStreamLen <= ITEM_SHORT_LENGTH) {
                            cmd = conn.TempUpdateShort;
                        }
                        else {
                            cmd = conn.TempUpdateShortNullLong;
                        }
                    }
                    else {
                        if (_rqOrigStreamLen <= ITEM_SHORT_LENGTH) {
                            cmd = conn.TempUpdateLongNullShort;
                        }
                        else {
                            cmd = conn.TempUpdateLong;
                        }
                    }

                }
                else {
                    if (length <= ITEM_SHORT_LENGTH) {
                        cmd = conn.TempInsertShort;
                    }
                    else {
                        cmd = conn.TempInsertLong;
                    }
                }

                cmd.Parameters[0].Value = id + _partitionInfo.AppSuffix;
                cmd.Parameters[1].Size = length;
                cmd.Parameters[1].Value = buf;
                cmd.Parameters[2].Value = item.Timeout;
                if (!newItem) {
                    cmd.Parameters[3].Value = lockCookie;
                }

                try {
                    cmd.ExecuteNonQuery();
                }
                catch (Exception e) {
                    HandleInsertException(conn.Connection, e, newItem, id);
                }
            }
            finally {
                DisposeOrReuseConnection(ref conn, usePooling);
            }
        }

        public override void RemoveItem(HttpContext context, 
                                        String id, 
                                        object lockId, 
                                        SessionStateStoreData item) {
            //Debug.Trace("SqlSessionStateStore", "Calling Sql Remove, id=" + id);
            //Debug.Assert(lockId != null, "lockId != null");
            //Debug.Assert(context != null, "context != null");

            bool                usePooling = true;
            SqlStateConnection  conn = null;
            int                 lockCookie = (int)lockId;

            try {
                //SessionIDManager.CheckIdLength(id, true /* throwOnFail */);

                conn = GetConnection(id, ref usePooling);
                try {
                    SqlCommand cmd = conn.TempRemove;
                    cmd.Parameters[0].Value = id + _partitionInfo.AppSuffix;
                    cmd.Parameters[1].Value = lockCookie;
                    cmd.ExecuteNonQuery();
                }
                catch (Exception e) {
                    ThrowSqlConnectionException(conn.Connection, e);
                }

            }
            finally {
                DisposeOrReuseConnection(ref conn, usePooling);
            }
        }

        public override void ResetItemTimeout(HttpContext context, String id) {
            //Debug.Trace("SqlSessionStateStore", "Calling Sql ResetTimeout, id=" + id);
            //Debug.Assert(context != null, "context != null");

            bool                usePooling = true;
            SqlStateConnection  conn = null;

            try {
                //SessionIDManager.CheckIdLength(id, true /* throwOnFail */);
                
                conn = GetConnection(id, ref usePooling);
                try {
                    SqlCommand cmd = conn.TempResetTimeout;
                    cmd.Parameters[0].Value = id + _partitionInfo.AppSuffix;
                    cmd.ExecuteNonQuery();
                }
                catch (Exception e) {
                    ThrowSqlConnectionException(conn.Connection, e);
                }
            }
            finally {
                DisposeOrReuseConnection(ref conn, usePooling);
            }
        }

        public override SessionStateStoreData CreateNewStoreData(HttpContext context, int timeout)
        {
            //Debug.Assert(context != null, "context != null");
            return CreateLegitStoreData(context, null, null, timeout);
        }

        public override void CreateUninitializedItem(HttpContext context, String id, int timeout) {
            //Debug.Trace("SqlSessionStateStore", "Calling Sql InsertUninitializedItem, id=" + id);
            //Debug.Assert(context != null, "context != null");

            bool                    usePooling = true;
            SqlStateConnection      conn = null;
            byte []                 buf;
            int                     length;

            try {
                //SessionIDManager.CheckIdLength(id, true /* throwOnFail */);

                // Store an empty data
                SerializeStoreData(CreateNewStoreData(context, timeout),
                                ITEM_SHORT_LENGTH, out buf, out length);
                
                conn = GetConnection(id, ref usePooling);
                
                try {
                    SqlCommand cmd = conn.TempInsertUninitializedItem;
                    cmd.Parameters[0].Value = id + _partitionInfo.AppSuffix;
                    cmd.Parameters[1].Size = length;
                    cmd.Parameters[1].Value = buf;
                    cmd.Parameters[2].Value = timeout;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -