2012-01-13 56 views
0

我創建了一個Web應用程序,在應用程序中需要大量的單詞,這需要花費大量的時間,並在需要編寫時思考。Asp .net會話超時,所有數據都沒有保存,有什麼想法?

讓我們假設會話超時30分鐘後,我開始寫了很多字,並同時思考和寫作會話超時和重定向到登錄頁面,所有寫入的數據都將丟失。

除了延長會話超時時間外,對於這個問題的任何想法?

+0

您可以更改會話超時在web.config 。另一種選擇可能是將數據保存到用戶的cookie中,因爲在會話超時時您不會丟失數據 – Mharlin 2012-01-13 10:58:26

回答

0

目前您的會話創建和In-Process模式管理,在這種模式下,一旦達到超時階段,你無法恢復會話狀態。您可以爲SQL Server Mode設置SQL Server Modeconfigure your application,這樣您的數據將被保存到Sql Server數據庫中。

Profile Properties是替代保存狀態。

0

可以使用一些Ajax功能,定期「電話回家」(在服務器上執行一些虛擬的代碼)。只要該用戶打開此頁面,這將使會話保持活動狀態。

您可能需要顯式地使用Session在回調,如

Session["LastAccess"] = DateTime.Now; 

只是爲了保持它活着。

如果執行此電話每隔15分鐘,會話不會超時和服務器上的負載是最小的。這允許一些代碼部分

0

使用異步編程模型到在單獨的線程上執行。

沒有與APM三個樣式編程的

  1. 等到完成型號

  2. 輪詢模型

  3. 回調模型

根據您的需要和結果你可以選擇更適合的模型ropriate。

例如,讓我們說你可以讀取該文件,並等待完成,示例代碼

byte[] buffer = new byte[100]; 
string filename = 
string.Concat(Environment.SystemDirectory, "\\mfc71.pdb"); 
FileStream strm = new FileStream(filename, 
FileMode.Open, FileAccess.Read, FileShare.Read, 1024, 
FileOptions.Asynchronous); 
// Make the asynchronous call 
strm.Read(buffer, 0, buffer.Length); 
IAsyncResult result = strm.BeginRead(buffer, 0, buffer.Length, null, null); 
// Do some work here while you wait 
// Calling EndRead will block until the Async work is complete 
int numBytes = strm.EndRead(result); 
// Don't forget to close the stream 
strm.Close(); 
Console.WriteLine("Read {0} Bytes", numBytes); 
Console.WriteLine(BitConverter.ToString(buffer)); 

但創建的線程是沒有必要或暗示,.NET支持內置的線程池可以用在你想要創建自己的線程的許多情況下。示例代碼

static void WorkWithParameter(object o) 
{ 
string info = (string) o; 
for (int x = 0; x < 10; ++x) 
{ 
Console.WriteLine("{0}: {1}", info, 
Thread.CurrentThread.ManagedThreadId); 
// Slow down thread and let other threads work 
Thread.Sleep(10); 
} 
} 

不是創建一個新線程並控制它,我們使用線程池對這項工作通過使用其QueueWorkItem方法

WaitCallback workItem = new WaitCallback(WorkWithParameter)); 
if (!ThreadPool.QueueUserWorkItem(workItem, "ThreadPooled")) 
{ 
Console.WriteLine("Could not queue item"); 
}