【问题标题】:How to prevent Idle Timeout in IIS Application Pool on a shared host如何防止共享主机上的 IIS 应用程序池中的空闲超时
【发布时间】:2017-11-07 01:22:59
【问题描述】:

我将我的 ASP.Net 应用程序上传到共享主机(godaddy plesk windows 服务器)。共享主机上的空闲超时设置为 5 分钟且无法更改。

我听说可以在超时期限过去之前通过 ping 服务器来重置会话计时器,我正在尝试这样做,但我的代码似乎不起作用。我以此为指导:

  1. Keeping ASP.NET Session Open / Alive(玛丽安的帖子) 和

  2. https://gist.github.com/KyleMit/aa4f576fa32bf36fbedab5540c18211d

基本上在我的 Home 控制器中,我已经输入了以下代码:

 [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 分钟空闲一次就会让我退出,我可以说应用程序池会回收,因为网站需要永远重新启动。我究竟做错了什么?我怎样才能让这个停止让我退出。

感谢任何信息或指出正确的方向。

【问题讨论】:

  • 用这个改变你的web.config,
  • 很遗憾,这没有帮助。

标签: jquery asp.net session iis hosting


【解决方案1】:

作为替代方案,您可以使用外部正常运行时间监控服务通过 GET 请求不断查询您的网站,这也将使其保持活动状态。大多数正常运行时间监控器实际上并不发送请求,但 Application Insights 中的 可用性 功能可以做到这一点并且非常适合。

我写了更多关于它here

【讨论】:

    【解决方案2】:

    这是通过在布局页面中插入一个 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();
    
    }
    

    【讨论】:

      【解决方案3】:

      我知道这个问题是不久前提出的,但我认为它可能对将来的某人有所帮助。

      我花了一整天的时间试图解决这个问题,直到我遇到了this article

      作者基本上创建了一个方法,您将其放入 Global.asax 文件的 Application_Start 方法中。每 60 秒上传一个空字符串到您的站点的方法(您可以根据需要更改秒数),以避免 5 分钟后出现空闲超时。它就像一个魅力。

      很遗憾,无法更改 godaddy plesk 服务器上的 IIS 设置。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-03
        • 2011-03-15
        • 2010-12-08
        • 2016-10-22
        • 1970-01-01
        • 2011-08-14
        • 1970-01-01
        • 2020-05-13
        相关资源
        最近更新 更多