Beispiel #1
0
        /// <summary>取得舊有或建立新的 CJF.Net.Http.Session 執行個體。</summary>
        /// <param name="context">傳入包裝著 System.Net.HttpListenerContext 殼的執行個體。</param>
        /// <param name="session">傳回已存在的 CJF.Net.Http.Session 執行個體。</param>
        /// <returns>true: 找到對應的 CJF.Net.Http.Session 執行個體; false: 未找到,但已建立新的 CJF.Net.Http.Session 執行個體。</returns>
        public bool GetOrCreate(HttpServiceContext context, out HttpListenerSession session)
        {
            var cookie = context.Request.Cookies[_cookieName];

            session = null;
            if (cookie != null && !cookie.Expired)
            {
                _sessions.TryGetValue(cookie.Value, out session);
                if (session != null)
                {
                    session.LastRequest = DateTime.Now;
                }
            }
            if (session == null)
            {
                HttpListenerSession sess = new HttpListenerSession();
                _sessions.AddOrUpdate(sess.ID, sess, (k, v) => v = sess);
                cookie = new Cookie(_cookieName, sess.ID, "/");
                context.Response.SetCookie(cookie);
                context.Session = sess;
                session         = sess;
                SessionStart?.BeginInvoke(this, context, null, null);
                return(false);
            }
            else
            {
                context.Session = session;
                return(true);
            }
        }
Beispiel #2
0
 /// <summary>嘗試移除 CJF.Net.Http.Session 物件。</summary>
 /// <param name="sessionId">Session ID</param>
 /// <param name="session">傳回 CJF.Net.Http.Session 物件</param>
 /// <returns>true: 已移除; false: sessionId 不存在。</returns>
 public bool TryRemove(string sessionId, out HttpListenerSession session)
 {
     if (!_sessions.TryRemove(sessionId, out session))
     {
         return(false);
     }
     SessionEnd?.BeginInvoke(this, sessionId, null, null);
     return(true);
 }