Asp .net会话超时,所有数据都没有保存,有什么想法?
问题描述:
我创建了一个Web应用程序,在应用程序中需要大量的单词,这需要花费大量的时间,并在需要编写时思考。Asp .net会话超时,所有数据都没有保存,有什么想法?
让我们假设会话超时30分钟后,我开始写了很多字,并同时思考和写作会话超时和重定向到登录页面,所有写入的数据都将丢失。
除了延长会话超时时间外,对于这个问题的任何想法?
答
目前您的会话创建和In-Process
模式管理,在这种模式下,一旦达到超时阶段,你无法恢复会话状态。您可以为SQL Server Mode
设置SQL Server Mode
和configure your application,这样您的数据将被保存到Sql Server数据库中。
Profile Properties是替代保存状态。
答
可以使用一些Ajax功能,定期“电话回家”(在服务器上执行一些虚拟的代码)。只要该用户打开此页面,这将使会话保持活动状态。
您可能需要显式地使用Session在回调,如
Session["LastAccess"] = DateTime.Now;
只是为了保持它活着。
如果执行此电话每隔15分钟,会话不会超时和服务器上的负载是最小的。这允许一些代码部分
答
使用异步编程模型到在单独的线程上执行。
没有与APM三个样式编程的
等到完成型号
轮询模型
回调模型
根据您的需要和结果你可以选择更适合的模型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");
}
您可以更改会话超时在web.config 。另一种选择可能是将数据保存到用户的cookie中,因为在会话超时时您不会丢失数据 – Mharlin 2012-01-13 10:58:26