我已将 ASP.Net 应用程序上传到共享主机(godaddy plesk windows 服务器)上。共享主机上的空闲超时设置为 5 分钟且无法更改。
我听说可以通过在超时期限结束之前对服务器执行 ping 操作来重置 session 计时器,我正在尝试执行此操作,但我的代码似乎不起作用。我以此为指导:
Keeping ASP.NET Session Open / Alive (玛丽安的帖子) 和
https://gist.github.com/KyleMit/aa4f576fa32bf36fbedab5540c18211d
基本上在我的家庭 Controller 中我放置了以下代码:
[HttpPost]
public JsonResult KeepSessionAlive()
{
return new JsonResult {Data = "Success"};
}
在我的脚本文件夹中,我放置了名为 SessionUpdater.js 的脚本
SessionUpdater = (function () {
var clientMovedSinceLastTimeout = true; //I want it to be always on so if statement fires
var keepSessionAliveUrl = null;
var timeout = 1 * 1000 * 60; // 1 minutes
function setupSessionUpdater(actionUrl) {
// store local value
keepSessionAliveUrl = actionUrl;
// setup handlers
listenForChanges();
// start timeout - it'll run after n minutes
checkToKeepSessionAlive();
}
function listenForChanges() {
$("body").one("mousemove keydown", function () {
clientMovedSinceLastTimeout = true;
});
}
// fires every n minutes - if there's been movement ping server and restart timer
function checkToKeepSessionAlive() {
setTimeout(function () { keepSessionAlive(); }, timeout);
}
function keepSessionAlive() {
// if we've had any movement since last run, ping the server
if (clientMovedSinceLastTimeout == true) {
$.ajax({
type: "POST",
url: keepSessionAliveUrl,
success: function (data) {
// reset movement flag
clientMovedSinceLastTimeout = true; //always on
// start listening for changes again
listenForChanges();
// restart timeout to check again in n minutes
checkToKeepSessionAlive();
},
error: function (data) {
console.log("Error posting to " & keepSessionAliveUrl);
}
});
}
}
// export setup method
return {
Setup: setupSessionUpdater
};
})();
然后在我的布局页面和我的主页\索引页面中(以防部分 View 由于某种原因不起作用)我放置了此引用:
<script type="text/javascript">
// initialize Session Updater on page
SetupSessionUpdater('/Home/KeepSessionAlive');
SessionUpdater.Setup('@Url.Action("KeepSessionAlive","Home")');
</script>
我的问题是,尽管这样做了, session 仍然会每 5 分钟空闲一次将我注销,并且我可以看出应用程序池会回收,因为网站需要很长时间才能再次启动。我究竟做错了什么?我怎样才能让它停止将我注销。
任何信息或指向正确方向的信息都将受到赞赏。
最佳答案
我知道这个问题不久前被问过,但我认为它可能对将来的某人有帮助。
我花了一整天的时间试图解决这个问题,直到我遇到 this article 。
作者基本上创建了一个方法,您可以将其放入 Global.asax 文件中的 Application_Start 方法中。该方法每 60 秒上传一个空字符串到您的网站(您可以根据需要更改秒数),以避免 5 分钟后出现空闲超时。它就像一个魅力。
不幸的是,无法更改 godaddy plesk 服务器上的 IIS 设置。
关于jquery - 如何防止共享主机上的 IIS 应用程序池空闲超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44383014/