0

我們ASP.NET C#Web應用程序在以下環境中使用Rare System.NullReferenceException顯示了以前填充的HttpContext.Current.Session [「courseNameAssociatedWithLoggedInUser」]?

  • .NET框架4
  • 的Silverlight 4/PivotViewer
  • IIS 7級
  • 的Windows 2008
  • 的Visual Studio .NET 2010 IDE
  • C#
  • HTTPS(SSL)

我們的Silverlight 4/PivotViewer控件顯然嵌入在 ASP.NET頁面中。

在極少數情況下,我們得到「未將對象引用設置爲 實例的對象。錯誤圍繞承載我們 的Silverlight在我們的獨立的Windows 2008年4月/ PivotViewer模塊與IIS 7 服務器ASP.NET頁面導航時

此外,還有很多關於錯誤的非常奇怪的事情,首先,它在有些用戶計算機上只出現有時只出現在 通常只有當用戶使用訪問我們的ASP的計算機時纔會發生。 NET網頁應用程序第一次。

Mor一旦我們清除了瀏覽器緩存,在導航我們的 Silverlight 4/PivotViewer模塊的ASP.NET頁面時,錯誤不會發生 。

有關錯誤的另一個有趣的方面是,它是指 絕對路徑的錯誤堆棧跟蹤 我的本地開發計算機上時,它是在現實中部署到獨立的Windows 2008服務器 與IIS 7 That`s真奇怪,因爲在我部署到 帶有IIS 7的獨立Windows 2008服務器之前,我確保文件(如web.config)中的所有 配置參數都參考 獨立服務器環境。換句話說,我刪除了任何 引用與我的本地 開發計算機相關的配置參數值。

因此,我很困惑,爲什麼它在我的 本地開發計算機上引用了錯誤堆棧跟蹤中的絕對路徑。

如果我們必須告訴用戶,如果用戶在使用該站點時得到「對象引用 未設置爲對象實例」的錯誤體驗,而且它也會是不好的用戶體驗要清除 他/她的瀏覽器緩存,每次錯誤顯示出來

Server Error in '/' 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: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.] PivotServer.SectionBasedRelatedModules.SectionBasedPerlsViewer.tailorConfigurationDetailsOfPageElementsToFallInLineWithTutorialSelection() in C:\VisualStudioWorkSpace\VisualCSharpProjects\PerlsPivot\PivotServer\SectionBasedRelatedModules\SectionBasedPerlsViewer.aspx.cs:160 PivotServer.SectionBasedRelatedModules.SectionBasedPerlsViewer.Page_Load(Object sender, EventArgs e) in C:\VisualStudioWorkSpace\VisualCSharpProjects\PerlsPivot\PivotServer\SectionBasedRelatedModules\SectionBasedPerlsViewer.aspx.cs:146 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +24 System.Web.UI.Control.LoadRecursive() +70 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3063

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272

我看着它包含下面的代碼行160:

String coursename = HttpContext.Current.Session["courseNameAssociatedWithLoggedInUser"].ToString(); 

顯然,這告訴我們,有否v在HttpContext.Current.Session中變量「courseNameAssociatedWithLoggedInUser」的變量。

但是,我在另一臺計算機上採取了相同的步驟,但一切順利。 而且,我相信,我在儘快給予一個值HttpContext.Current.Session [「courseNameAssociatedWithLoggedInUser」]作爲用戶登錄。

是否HttpContext.Current.Session [「courseNameAssociatedWithLoggedInUser」]會損壞因爲某些原因?如果是這樣,爲什麼,我們如何防止它?

我應該使用Page.Session而不是HttpContext.Current.Session?這會有幫助嗎?

+1

中或在呼叫者/不變量某處代碼中的錯誤:'PivotServer.SectionBasedRelatedModules.SectionBasedPerlsViewer.tailorConfigurationDetailsOfPageElementsToFallInLineWithTutorialSelection()'。當然,開發人員將不得不隔離究竟在哪裏/是什麼罪魁禍首。我建議使用調試器。如果「清除緩存」似乎「解決問題」,這聽起來像是要調查的東西..也許一些不良的數據/不變量被存儲,或試圖恢復文化依賴的性質。 – 2013-03-18 17:36:10

+1

幾乎所有'NullReferenceException'的情況都是一樣的。請參閱「[什麼是.NET中的NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)」的一些提示。 – 2013-03-18 17:40:59

+1

你可以發佈異常網站的代碼嗎? SectionBasedPerlsViewer.aspx.cs - 其中有160行的方法? – StingyJack 2013-03-18 17:41:01

回答

3

"Obviously, this tells us that there is no value for the variable"courseNameAssociatedWithLoggedInUser" in the HttpContext.Current.Session."

不完全是......給你的代碼...

String coursename = HttpContext.Current.Session["courseNameAssociatedWithLoggedInUser"].ToString(); 

...下列項目可能會導致這

  • HttpContext的 - 不可能
  • HttpContext.Current - 比您想象的更頻繁,但可以重現
  • HttpContext.Current.Session [「courseNameAssoc iatedWithLoggedInUser「]

要找出在鏈中的特定項目,你可以做這樣的事情(僞代碼)

function string GetCourseNameThingie() 
{ 
    if HttpContext is null 
    throw new Exception("HttpContext is null") 
    if HttpContext.Current is null 
    throw new Exception("HttpContext.Current is null") 
    if HttpContext.Current.Session["courseNameAssociatedWithLoggedInUser"] is null 
    throw new Exception("HttpContext.Current.Session["courseNameAssociatedWithLoggedInUser"]is null") 

    return HttpContext.Current.Session["courseNameAssociatedWithLoggedInUser"].ToString() 
} 

這會明確告訴你少了什麼。正如我們已經指出的那樣,它更有可能被另一個頁面清除,或者發生的錯誤嚴重到足以銷燬該用戶的會話(在Global.asax中是否有Application_Error或Session_End處理程序?)。

您也可以嘗試將try/catch中的原始語句包裝起來,如果發生異常,請立即檢查另一個您認爲仍在工作的會話變量。這也會告訴你確定其他變量是否正確。

1

首先,對不起我的語法 - 英語。

我和我的.net應用程序有類似的問題,原因是空閒時間到期。

有四個時間到期設置「(是4)」和應用程序池的回收過程。我做了以下操作

A)從應用程序池:打開提前設置並更改「空閒超時」。默認是20分鐘。

此外,將「定時時間間隔」設置爲0.因此它不會被回收。我不知道爲什麼微軟決定將其默認爲1740分鐘。這不是一個超時設置,它是一段時間後總是回收appPool的設置,所以如果你的用戶處於某種事情的中間,他們將會失去一切,或者像你所擁有的那樣有例外。將其設置爲0將禁用此功能。 B)認證超時。如果您正在使用它,例如表單身份驗證,那麼請轉到您的虛擬目錄應用程序的身份驗證功能,右鍵單擊「表單身份驗證」並選擇「編輯」。更改身份驗證Cookie超時。C)ASP.NET會話狀態。在ISS中,打開.net會話狀態功能並更改會話超時。這也是默認的20分鐘。 D)asp.net表單身份驗證超時。這在開始時讓我感到困惑,因爲我已經在IIS中修改了表單身份驗證的cookie時間(步驟B)。所以,我仍然有一些問題,當我意識到我還需要改變web.config文件形式認證標籤爲了增加超時屬性改變默認的20分鐘。

我的標籤,看起來就像是: 「ASPXFORMSAUTH」 ......構成名=路徑= 「/」 loginUrl = 「frmLogin.aspx」 超時= 「1740」保護= 「所有」 ......

我們開發了一種JavaScript機制,它可以使會話狀態超時設置,所以我們熱情的用戶表示系統將因不活動而自動註銷。

現在它美麗的,基本上我已經將所有超時設置爲「1740」分鐘,除了我們設置爲30分鐘的會話狀態。因此,在不活動的第29分鐘,我們向用戶顯示系統將在一分鐘內自動註銷,除非他們執行了一些活動,所以空閒時間再次從0開始。很棒,現在我們沒有任何問題。

反正傢伙,我已經告訴你我們的經驗,以及我們如何解決,你現在有類似的問題。然而,我之所以在這裏結束的原因是因爲我們也想在我的本地開發計算機上刪除任何異常時對絕對路徑的引用。

爲什麼它顯示的絕對路徑,我們還沒有想出。我們已經在發佈模式下編譯了我們的DLL程序集,但是這並沒有改變任何東西。讓你的某個人把這個問題弄清楚?????

謝謝你們

1

引用一個會話在一個視圖模型

您可能沒有使用ASP.net MVC,但如果有人這將是有益的尋找答案。

我是完全一樣的情況下,除了我每次接收空例外,而我的同事一點都沒有,即使我們運行完全相同的代碼。

我們的錯誤是從一個ViewModel,這顯然是不好的做法,引用一個會話變量。代碼已更改,以便ViewModel通過控制器接收會話內容,然後將其設置爲ViewModel的屬性。會話數據(我的例子稱爲ProcessingWeek)將使用this.ProcessingWeek,而不是直接查看httpcontext.current.session [「ProcessingWeek」]。

EventMatchResult Match = new EventMatchResult(
     (ImportedEventModel)SessionData.ImportedEventModel, 
     ref db, 
     SessionData.ProcessingWeek); 

...

public EventMatchResult(ImportedEventModel Event, ref CFModel db, 
                int ProcessingWeek) 
    { 
     this.db = db; 
     this.MatchedField = new List<PlayerMatchResult>(); 
     this.ImportedEvent = Event; 
     this.ProcessingWeek = ProcessingWeek; 
    }