将OrderedDictionary保存/加载到会话

时间:2011-06-02 11:27:42

标签: c# .net asp.net session ordereddictionary

我正在尝试将OrderedDictionary保存到会话中并重新加载它。 基本上,它是一个“最后播放”的游戏列表。

由于某种原因,字典是新的....任何人都可以找出原因吗?

<%@ 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 context) {

        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."); }       
    }

}

2 个答案:

答案 0 :(得分:3)

好的,所以我发现了问题, 我正在使用IReadOnlySessionState 而不是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."); }

    }

}

编辑:发表完整的工作答案

答案 1 :(得分:1)

避免双容器查找:

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 
}