这是我最终得到的解决方案:
(在https://stackoverflow.com/q/1470571/126785 和 Ken Egozi 的 cmets 的帮助下)
在 Global.asax.cs 中:
private static readonly object padlock = new object();
private static Dictionary<string,SessionData> sessions = new Dictionary<string,SessionData>();
public static Dictionary<string, SessionData> Sessions
{
get { lock (padlock) { return sessions; } }
}
public struct SessionData
{
public string Name { get; set; }
public int AccountId { get; set; }
public string CurrentLocation { get; set; }
}
protected void Session_Start(object sender, EventArgs e)
{
Sessions.Add(Session.SessionID, new SessionData());
}
protected void Session_End(object sender, EventArgs e)
{
Sessions.Remove(Session.SessionID);
}
public static void SetSessionData(string sessionId, int accountId, string name, string currentLoc)
{
Sessions.Remove(sessionId);
Sessions.Add(sessionId, new SessionData { AccountId = accountId, CurrentLocation = currentLoc, Name = name });
}
public static void SetCurrentLocation(string sessionId, string currentLoc)
{
SessionData currentData = Sessions[sessionId];
Sessions.Remove(sessionId);
Sessions.Add(sessionId, new SessionData { AccountId = currentData.AccountId, CurrentLocation = currentLoc, Name = currentData.Name });
}
然后登录时:
Global.SetSessionData(((HttpSessionStateContainer)Session.SyncRoot).SessionID,account.Id,account.Name,"Logged In");
现在我只需要找出更新位置的最佳位置。每个函数的调用可能有点烦人!