저는 몇 개의 클래스에 대해 HttpContext.Current 속성처럼 동작하도록 정적 "현재"속성을 구현하려고했습니다. 컨텍스트에만 의존하거나 요청 헤더는 정상적으로 작동합니다. 그러나 현재 Session 개체에 종속 된 개체는 실패합니다. 이러한 경우, 세션 개체가 null로 나타납니다.NET - 정적 메서드에서 세션 개체가 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() 메소드에서 세션의 보류를 얻을 수 없습니다.
의견이 있으십니까?
감사합니다.
이 코드를 실제 코드로 업데이트 할 수 있습니다. 내일 내가 다시 사무실에있을 때. 아마 당신이 기대하는 것 일 겁니다. 하지만, 어쨌든 모든 사람들이 예상대로 작동하는지 확인하기 위해 게시 할 것입니다. 사실, 우주에 질서가 있습니다 ... – svidgen