如何防止共享主机上 IIS 应用程序池中的空闲超时
How to prevent Idle Timeout in IIS Application Pool on a shared host
我的 ASP.Net 应用程序已上传到共享主机(godaddy plesk windows 服务器)。共享主机上的空闲超时设置为 5 分钟且无法更改。
我听说可以在超时期限结束之前通过对服务器执行 ping 操作来重置会话计时器,我正在尝试这样做,但我的代码似乎不起作用。我以此为指导:
Keeping ASP.NET Session Open / Alive(Maryan 的 post)
和
https://gist.github.com/KyleMit/aa4f576fa32bf36fbedab5540c18211d
基本上在我的 Home 控制器中我放了这段代码:
[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
};
})();
然后在我的布局页面和我的 home\index 页面中(以防部分视图由于某种原因不起作用)我把这个参考:
<script type="text/javascript">
// initialize Session Updater on page
SetupSessionUpdater('/Home/KeepSessionAlive');
SessionUpdater.Setup('@Url.Action("KeepSessionAlive","Home")');
</script>
我的问题是,尽管这样做了,会话仍然每 5 分钟空闲一次注销我,我可以看出应用程序池正在回收,因为网站需要很长时间才能再次启动。我究竟做错了什么?我怎样才能让这个停止注销。
感谢任何信息或指向正确方向的信息。
我知道不久前有人问过这个问题,但我想这可能对以后的人有所帮助。
我花了一整天的时间试图解决这个问题,直到遇到 this article。
作者基本上是在 Application_Start 方法中创建了一个方法,您将其放入 Global.asax 文件中。每 60 秒向您的站点上传一个空字符串的方法(您可以根据需要更改秒数),以避免 5 分钟后出现空闲超时。它就像一个魅力。
很遗憾,无法更改 Godaddy plesk 服务器上的 IIS 设置。
这是通过在布局页面中插入一个 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();
}
作为替代方案,您可以使用外部正常运行时间监控服务,通过 GET 请求持续查询您的网站,这也将使它保持活动状态。大多数正常运行时间监视器实际上并不发送请求,但是 Application Insights 中的 Availability 功能可以完美地完成此任务。
我写了更多关于它的文章here。
我的 ASP.Net 应用程序已上传到共享主机(godaddy plesk windows 服务器)。共享主机上的空闲超时设置为 5 分钟且无法更改。
我听说可以在超时期限结束之前通过对服务器执行 ping 操作来重置会话计时器,我正在尝试这样做,但我的代码似乎不起作用。我以此为指导:
Keeping ASP.NET Session Open / Alive(Maryan 的 post) 和
https://gist.github.com/KyleMit/aa4f576fa32bf36fbedab5540c18211d
基本上在我的 Home 控制器中我放了这段代码:
[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
};
})();
然后在我的布局页面和我的 home\index 页面中(以防部分视图由于某种原因不起作用)我把这个参考:
<script type="text/javascript">
// initialize Session Updater on page
SetupSessionUpdater('/Home/KeepSessionAlive');
SessionUpdater.Setup('@Url.Action("KeepSessionAlive","Home")');
</script>
我的问题是,尽管这样做了,会话仍然每 5 分钟空闲一次注销我,我可以看出应用程序池正在回收,因为网站需要很长时间才能再次启动。我究竟做错了什么?我怎样才能让这个停止注销。
感谢任何信息或指向正确方向的信息。
我知道不久前有人问过这个问题,但我想这可能对以后的人有所帮助。
我花了一整天的时间试图解决这个问题,直到遇到 this article。
作者基本上是在 Application_Start 方法中创建了一个方法,您将其放入 Global.asax 文件中。每 60 秒向您的站点上传一个空字符串的方法(您可以根据需要更改秒数),以避免 5 分钟后出现空闲超时。它就像一个魅力。
很遗憾,无法更改 Godaddy plesk 服务器上的 IIS 设置。
这是通过在布局页面中插入一个 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();
}
作为替代方案,您可以使用外部正常运行时间监控服务,通过 GET 请求持续查询您的网站,这也将使它保持活动状态。大多数正常运行时间监视器实际上并不发送请求,但是 Application Insights 中的 Availability 功能可以完美地完成此任务。
我写了更多关于它的文章here。