Analytics

2010年9月9日 星期四

[VISUAL STUDIO]解決 Session state has created a session id, but cannot save it because the response was already flushed by the application.


問題

Session state has created a session id, but cannot save it because the response was already flushed by the application.

ASP.NET1.1 Updrade到 ASP.NET3.5後,
當Publish Website後,發現網頁執行起來沒多久,Session會幾秒後就自動不見,
導致網頁一執導回首頁!!
從IIS裡看web Config的Session timeout預設也一樣是20分鐘!

從EventViewer發現這個問題:

Session state has created a session id, but cannot save it because the response was already flushed by the application.
查了很多資料以後發現,從ASP.NET 1.1升級後都有這種狀況
而都有相同的狀況就是有關Session的部分有在Global.cs裡加入程式碼!!


解決方法

(在以下事件裡加入一行code)
void Session_Start(object sender, EventArgs e) 
{      
            // Code that runs when a new session is started         
            //Ensure SessionID in order to prevent the folloing exception          
            //when the Application Pool Recycles          
            //[HttpException]: Session state has created a session id, but cannot           
            //       save it because the response was already flushed by    
            string sessionId = Session.SessionID;      
}
加入這行code的原因是當session id因某種原因被flushed 後,
而session再次被指派/產生後,
會造成無法將session id 識別給要新產的session而發生!!
而解決方式就是先暫時記住session id

沒有留言:

熱門文章