简体   繁体   中英

How to prevent Idle Timeout in IIS Application Pool on a shared host

I have my ASP.Net application uploaded on a shared host (godaddy plesk windows server). The Idle timeout on the shared host is set to 5 minutes and cannot be changed.

在此输入图像描述

I have heard that the session timer can be reset by pinging the server before the timeout period has elapsed and I am trying to do this but my code doesn't seem to work. I am using this as a guideline:

  1. Keeping ASP.NET Session Open / Alive (The post by Maryan) and

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

Basically in my Home controller I have put this code:

 [HttpPost]
 public JsonResult KeepSessionAlive()
 {
     return new JsonResult {Data = "Success"};
 }

In my Scripts folder I have put this script titled 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
};
 })();

And then in both my layout page and in my home\\index page (in case partial view doesnt work for some reason) I put this reference:

 <script type="text/javascript">
// initialize Session Updater on page
SetupSessionUpdater('/Home/KeepSessionAlive');
SessionUpdater.Setup('@Url.Action("KeepSessionAlive","Home")');
 </script>

My Problem is that despite doing this the session still logs me out every 5 minutes of being idle and I can tell that the app pool recycles because the website takes forever to fire up again. What am I doing wrong? How can I get this to stop logging me out.

Any information or being pointed in the right direction is appreciated.

I know this question was asked a while ago but I figured it might be helpful for someone in the future.

I just spent a full day trying to solve this issue, until I came across this article .

The author basically creates a method that you put in your Global.asax file in the Application_Start method. The method that uploads an empty string to your site every 60 seconds (you can change the amount of seconds if you want) ,to avoid there being an idle timeout after 5 minutes. It worked like a charm.

Unfortunately, there is no way of changing IIS settings on godaddy plesk server.

As an alternative, you can use an external uptime monitor service to continuously query your website with GET requests, which will also keep it alive. Most uptime monitors don't actually send requests, however the Availability feature in Application Insights does and works perfectly for this.

I wrote more about it here .

This was solved by inserting a javascript function in the layout page that pings the server every x amount of seconds.

Added this in layout (yes its disorganized, I never went back and checked which was working):

<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>

It references this 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();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM