Can you prevent your ASP.NET application from shutting down?

后端 未结 4 2034
长情又很酷
长情又很酷 2020-11-28 12:58

I think I heard that ASP.NET applications will shut down after a while of being idle (i.e. no visitors).

Is there a way to prevent this behavior from happening? I h

4条回答
  •  眼角桃花
    2020-11-28 13:10

    Besides external keep alive you can make an internal keep alive inside global.asax:

        static Thread keepAliveThread = new Thread(KeepAlive);
    
        protected void Application_Start()
        {
            keepAliveThread.Start();
        }
    
        protected void Application_End()
        {
            keepAliveThread.Abort();
        }
    
        static void KeepAlive()
        {
            while (true)
            {
                WebRequest req = WebRequest.Create("http://www.mywebsite.com/DummyPage.aspx");
                req.GetResponse();
                try
                {
                    Thread.Sleep(60000);
                }
                catch (ThreadAbortException)
                {
                    break;
                }
            }
        }
    

提交回复
热议问题