いくつかのクラスで、静的な「Current」プロパティを実装して、HttpContext.Current プロパティのように動作させようとしました。コンテキストまたはリクエスト ヘッダーのみに依存するものは、通常どおり動作します。ただし、現在の Session オブジェクトに依存するものは失敗します。これらの場合、Session オブジェクトは null のように見えます。
// THIS WORKS
private static HttpContext Context { get { return HttpContext.Current; } }
// THIS APPEARS TO YIELD NULL
private static HttpSessionState Session { get { return HttpContext.Current.Session; } }
public static EducationalUnit Current
{
get
{
if (Context.Items["EducationalUnit.Current"] == null)
{
SetCurrent();
}
return (EducationalUnit)Context.Items["EducationalUnit.Current"];
}
set
{
Context.Items["EducationalUnit.Current"] = value;
}
} // Current
// I've tried a few things here, to scope out the status of the Session:
private static void SetCurrent()
{
// shows "null"
throw new Exception(Session);
// shows "null"
throw new Exception(Session.SessionID);
// also shows "null"
throw new Exception(HttpContext.Current.Session);
// shows "Object reference not set to an instance of an object."
throw new Exception(HttpContext.Current.Session.SessionID);
// this, however, properly echos my cookie keys!
JavaScriptSerializer js = new JavaScriptSerializer();
throw new Exception(js.Serialize(Context.Request.Cookies.Keys.ToString()));
} // SetCurrent()
私の人生では、 SetCurrent() メソッドからセッションを取得することはできません。
何かご意見は?
ありがとう!