2014-02-06 5 views
0

코드 숨김 페이지와 .asp 페이지와 관련이없는 별도의 클래스가 모두있는 ASP 웹 응용 프로그램에서 작업하고 있습니다. 코드 숨김 페이지에서 몇 가지 세션 변수를 관리합니다. 그 중 하나는 Session ("debug")입니다. 예를 들어 : public 클래스 중 하나에서VB.NET : 공용 클래스에서 세션 변수에 액세스하는 방법?

'Have we created a session debug variabe yet? If not, create one! 
    If Session("debug") IsNot Nothing Then 
     _debug = TryCast(Session("debug"), DebugControl) 
    Else 
     _debug = New DebugControl 
     Session("debug") = _debug 
    End If 
    'This is the debug control. Uncomment this to turn debugging on 
    _debug.isOn = True 

, 나는 클래스 생성자에서 프로그래밍 세션 변수를 사용하려고 : 나는 응용 프로그램을 빌드 할 때

Public Class SQLControl 
     Inherits System.Web.UI.MasterPage 
      Private _debug As DebugControl 

      Public Sub New() 
       If HttpContext.Current.Session("debug") Is Nothing Then 
        _debug = New DebugControl 
        HttpContext.Current.Session("debug") = _debug 
       Else 
        _debug = DirectCast(HttpContext.Current.Session("debug"), DebugControl) 
       End If 
      End Sub 

그러나, 나는 (다음을 얻을 라인 26) 오류로 강조 표시됩니다 : 나는 비슷한 문제를 설명하는 여러 스레드를 읽었습니다

Server Error in '/dev' Application. 

Object reference not set to an instance of an object. 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. 

Source Error: 


Line 24: 
Line 25:  Public Sub New() 
Line 26:   If HttpContext.Current.Session("debug") Is Nothing Then 
Line 27:    _debug = New DebugControl 
Line 28:    HttpContext.Current.Session("debug") = _debug 

,하지만 그들 중 대부분은 HttpContext.Current.Session() 메소드를 사용하여 제안하는 I 이미하고있다. (같은 결과로 System.Web.HttpContext.Current.Session을 시도했다.) 어떤 생각이든?

감사합니다.

"별도의 클래스가 어떤 .ASP [원문] 페이지와 관련이없는"경우와 데이비드

+0

매개 변수에 현재 세션을 전달하지 않는 이유가 있습니까? –

+0

글쎄, 모든 클래스의 모든 메소드 (첫 번째 실패 디버그 로그를 유지하기위한 메소드)에 의해 debugControl의 인스턴스를 작성해야하므로 모든 함수 호출시이를 오버 플로우하는 것처럼 보입니다. – dtbritt

답변

0

당신은 그들이 WebService에 대한 방법을 포함하고 있음을 암시됩니다, 당신은 그것을 말할 수있는 당신은 장식 require use of the session state, 예를 들어,

<WebMethod(EnableSession:=True)> _ 
Public Function AddToBasket(ByVal filename As String) As String 
    Return BasketController.AddItem(filename, HttpContext.Current).ToString 
End Function 
+0

아니요, 웹 서비스가 아닙니다. 그것은 단지 .vb 파일의 App_Code 디렉토리에 있습니다. 어쨌든 webMethod를 시도했지만 다른 문제가 발생했습니다. – dtbritt

관련 문제