我将ASP.Net应用程序上传到共享主机(godaddy plesk windows server)。共享主机上的空闲超时设置为5分钟,无法更改。
我听说会话计时器可以通过在超时时间段之前ping服务器来重置,我试图这样做,但我的代码似乎没有用。我将此作为指导原则:
Keeping ASP.NET Session Open / Alive(玛丽安的帖子) 以及
https://gist.github.com/KyleMit/aa4f576fa32bf36fbedab5540c18211d
基本上在我的家庭控制器中我已经放了这段代码:
[HttpPost]
public JsonResult KeepSessionAlive()
{
return new JsonResult {Data = "Success"};
}
在我的Scripts文件夹中,我把这个脚本命名为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
};
})();
然后在我的布局页面和我的主页\索引页面中(如果部分视图由于某种原因不起作用)我把这个引用:
<script type="text/javascript">
// initialize Session Updater on page
SetupSessionUpdater('/Home/KeepSessionAlive');
SessionUpdater.Setup('@Url.Action("KeepSessionAlive","Home")');
</script>
我的问题是,尽管这样做,会话仍然每隔5分钟就会让我离开,我可以告诉应用程序池回收,因为该网站需要永远再次启动。我究竟做错了什么?我该如何让它停止让我退出。
赞赏任何信息或指向正确的方向。
答案 0 :(得分:2)
我知道这个问题是在不久之前被问过的,但我认为这可能会对将来有所帮助。
我花了整整一天时间试图解决这个问题,直到我遇到this article。
作者基本上创建了一个放在Application_Start方法中的Global.asax文件中的方法。每隔60秒将空字符串上传到站点的方法(如果需要,可以更改秒数),以避免在5分钟后出现空闲超时。它就像一个魅力。
不幸的是,没有办法在godaddy plesk服务器上更改IIS设置。
答案 1 :(得分:1)
作为替代方案,您可以使用外部正常运行时间监视器服务来持续使用GET请求查询您的网站,这也将使其保持活动状态。大多数正常运行时间监视器实际上并未发送请求,但 Application Insights 中的可用性功能可以完美地完成此操作。
我写了更多关于它的信息here。
答案 2 :(得分:0)
这是通过在布局页面中插入一个javascript函数来解决的,该函数每隔x秒对服务器进行一次ping操作。
在布局中添加了这个(是的,它没有组织,我从未回过头来检查哪个有效):
<script type="text/javascript" src="/scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/SessionUpdater.js"></script>
<script type="text/javascript">
// initialize Session Updater on page
SetupSessionUpdater('/Home/KeepSessionAlive');
SessionUpdater.Setup('@Url.Action("KeepSessionAlive","Home")');
</script>
<script type="text/javascript">
function keepAliveFunc() {
setTimeout("keepAlive()", 10000);
};
function keepAlive() {
$.post("/Home/KeepAlive", null, function () { keepAliveFunc(); });
};
$(keepAliveFunc());
</script>
它引用了这个脚本〜/ Scripts / SessionUpdater.js:
var keepSessionAlive = false;
var keepSessionAliveUrl = null;
function SetupSessionUpdater(actionUrl) {
keepSessionAliveUrl = actionUrl;
var container = $("#body");
container.mousemove(function () { keepSessionAlive = true; });
container.keydown(function () { keepSessionAlive = true; });
CheckToKeepSessionAlive();
}
function CheckToKeepSessionAlive() {
setTimeout("KeepSessionAlive()", 200000);
}
function KeepSessionAlive() {
if (keepSessionAlive && keepSessionAliveUrl != null) {
$.ajax({
type: "POST",
url: keepSessionAliveUrl,
success: function () { keepSessionAlive = false; }
});
}
CheckToKeepSessionAlive();
}