基本信息
源码名称:asp.net 分布式存储Session实例源码(mongodb实现)
源码大小:10.75M
文件格式:.zip
开发语言:C#
更新时间:2014-12-24
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
将会话信息信息分布存储至多台服务器,详见中的 WebFormDemo 项目

    internal class Helper
    {
        public static bool ParseSessionID(string rawSessionId, out string prefix, out string realSessionId)
        {
            prefix = string.Empty;
            realSessionId = string.Empty;

            if (rawSessionId == null)
                return false;
            if (rawSessionId.Trim().Length == 0)
                return false;
            string[] parsedValues = rawSessionId.Split('.');
            if (parsedValues == null)
                return false;
            if (parsedValues.Length != 2)
                return false;

            prefix = parsedValues[0];
            realSessionId = parsedValues[1];

            return true;
        }

        public static MongoCollection<MongoDBSessionEntity> GetMongoDBCollection(string sessionId)
        {
            IPartitionResolver resolver = new MongoDBSessionPartitionResolver();
            string mongoDbConnectionString = resolver.ResolvePartition(sessionId);

            MongoClient client = new MongoClient(mongoDbConnectionString);
            MongoServer srv = client.GetServer();
            MongoDatabase db = srv.GetDatabase(SessionConfiguration.MongoDBName);
            if (!db.CollectionExists(SessionConfiguration.MongoDBCollectionName))
                db.CreateCollection(SessionConfiguration.MongoDBCollectionName);

            MongoCollection<MongoDBSessionEntity> collection = db.GetCollection<MongoDBSessionEntity>(SessionConfiguration.MongoDBCollectionName);

            return collection;
        }

        public static string Serialize(SessionStateItemCollection items)
        {
            MemoryStream ms = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(ms);

            if (items != null)
                items.Serialize(writer);

            writer.Close();

            return Convert.ToBase64String(ms.ToArray());
        }


        public static SessionStateStoreData Deserialize(HttpContext context, string serializedItems, int timeout)
        {
            MemoryStream ms = null;
            if(serializedItems!=null&&serializedItems.Trim().Length>0)
                ms = new MemoryStream(Convert.FromBase64String(serializedItems));

            SessionStateItemCollection sessionItems =
              new SessionStateItemCollection();

            if (ms!=null&&ms.Length > 0)
            {
                BinaryReader reader = new BinaryReader(ms);
                sessionItems = SessionStateItemCollection.Deserialize(reader);
            }

            return new SessionStateStoreData(sessionItems,
              SessionStateUtility.GetSessionStaticObjects(context),
              timeout);
        }

        public static SessionStateItemCollection DeserializeCore(string serializedItems)
        {
            SessionStateItemCollection sessionItems = new SessionStateItemCollection();

            MemoryStream ms = null;
            if (serializedItems != null && serializedItems.Trim().Length > 0)
                ms = new MemoryStream(Convert.FromBase64String(serializedItems));

            if (ms != null && ms.Length > 0)
            {
                BinaryReader reader = new BinaryReader(ms);
                sessionItems = SessionStateItemCollection.Deserialize(reader);
            }

            return sessionItems;
        }
    }