开发者

Saving/Loading OrderedDictionary to Session

I am trying to save the OrderedDictionary into the session and reload it. basically, it is a "Last Played" game list.

for some reason, the dictionary is allays NEW .... anyone can pinpoint the reason?

<%@ WebHandler Language="C#" Class="LastPlayed" %>

using System;
using System.Web;
using System.Web.SessionState;

public class LastPlayed : IHttpHandler, IReadOnlySessionState
{
    public bool IsReusable { get { return false; } }

    public void ProcessRequest (HttpContext c开发者_开发技巧ontext) {

        context.Response.ContentType = "text/plain";

        string GameTitle = context.Request["gt"];
        string GameAlias = context.Request["ga"];

        System.Collections.Specialized.OrderedDictionary GamesDictionary = null;

        if (context.Session["LastPlayed"] != null)
        {
            // Load Dictionary from Session
            GamesDictionary = (System.Collections.Specialized.OrderedDictionary)context.Session["LastPlayed"];
            context.Response.Write("Loaded from Session");
        }
        else
        {
            // Creates and initializes a OrderedDictionary.
            GamesDictionary = new System.Collections.Specialized.OrderedDictionary();
            context.Response.Write("Created New Dictionary");
        }

        try
        {
            if (GamesDictionary.Count >= 5)
                // Remove the last entry from the OrderedDictionary
                GamesDictionary.RemoveAt(GamesDictionary.Count - 1);
            // Insert a new key to the beginning of the OrderedDictionary
            GamesDictionary.Insert(0, GameTitle, GameAlias);
            context.Session["LastPlayed"] = GamesDictionary;
            context.Response.Write("Added");
        }
        catch { context.Response.Write("Duplicate Found."); }       
    }

}


Ok, so i found the problem, i was using IReadOnlySessionState Instead of IRequiresSessionState

<%@ WebHandler Language="C#" Class="LastPlayed" %>

using System;
using System.Web;
using System.Web.SessionState;

public class LastPlayed : IHttpHandler, IRequiresSessionState
{
    public bool IsReusable { get { return false; } }

    public void ProcessRequest (HttpContext context) {

        context.Response.ContentType = "text/plain";

        string GameTitle = context.Request["gt"];
        string GameAlias = context.Request["ga"];

        System.Collections.Specialized.OrderedDictionary GamesDictionary = (System.Collections.Specialized.OrderedDictionary)context.Session["LastPlayed"];

        if (context.Session["LastPlayed"] == null)
            // Creates and initializes a OrderedDictionary.
            GamesDictionary = new System.Collections.Specialized.OrderedDictionary();       
        try
        {
            if (GamesDictionary.Count >= 5)
                // Remove the last entry from the OrderedDictionary
                GamesDictionary.RemoveAt(GamesDictionary.Count - 1);
            // Insert a new key to the beginning of the OrderedDictionary
            GamesDictionary.Insert(0, GameTitle, GameAlias);
            context.Session["LastPlayed"] = GamesDictionary;
            context.Response.Write("Added");
        }
        catch { context.Response.Write("Duplicate Found."); }

    }

}

EDIT: Posted full working answer


Avoid double container lookup:

var GamesDictionary = context.Session["LastPlayed"] as OrderedDictionary;
if (GamesDictionary != null)
{
    // do stuff
}
else
{
    // create new
    GamesDictionary = new OrderedDictionary();

    // probably! - put it inside
    context.Session["LastPlayed"] = GamesDictionary 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜