locked
need httpwebrequest to keep application alive RRS feed

  • Question

  • User-296603942 posted

    Hi,

    I want to write some code to keep my application alive (it's a a web host so I can't do it via normal web.config or global.asax stuff). However, I have no idea of how to write it or where I should put it. The reason is that otherwise my visitors will have to wait for 20 seconds for the pages to load due to recompiling or so.

    Ideally, I'd like to "call" my web site once every 21 minutes (the server's set to 20 so an extra minute provides time for an occasional restart if necessary due to memory hogs or the like).

    I've been struggling with this matter for many days now - your help will be extremely appreciated!

    Pettrer 

    Sunday, March 25, 2007 2:53 PM

Answers

  • User-1087479560 posted
    Hello Pettrer,

    My advice is you can start a timer to visit your application every 20 minuates in Applicaiton_start in global.asax.

    void Application_Start(object sender, EventArgs e)
    {
    System.Timers.Timer tm = new System.Timers.Timer(20 * 60 * 1000);
    tm.Elapsed += new System.Timers.ElapsedEventHandler(tm_Elapsed);
    }

    void tm_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
    System.Net.WebRequest rqst = System.Net.WebRequest.Create("the url of your website");
    rqst.GetResponse();
    }

    Please feel free to let me know if there is any problem.



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 26, 2007 2:41 AM
  • User-1573490007 posted
    For the record, the application will recycle on its own, no matter what, after it reaches a certain amount of memory. So you don't need to leave room for the occasional recycle, ASP.NET will -take- room, even if you're getting 100 hits/minutes :)
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 26, 2007 7:39 AM

All replies

  • User187056398 posted

    You can use the task scheduler: 

            Programs->Accessories->System Tools->Scheduled Tasks.

    Launch a browser with a web site url as an argument:

            C:\PROGRA~1\INTERN~1\iexplore.exe    www.microsoft.com

    It looks like you can only schedule a task to run once a day, but if you drill down into the advanced buttons, you can have it run the task every 20 minutes.

    By the way, your code is compiled the first time it is accessed, after that it remains compiled.  Only the first user would experience a delay due to compiling.

     

    Sunday, March 25, 2007 5:30 PM
  • User-1087479560 posted
    Hello Pettrer,

    My advice is you can start a timer to visit your application every 20 minuates in Applicaiton_start in global.asax.

    void Application_Start(object sender, EventArgs e)
    {
    System.Timers.Timer tm = new System.Timers.Timer(20 * 60 * 1000);
    tm.Elapsed += new System.Timers.ElapsedEventHandler(tm_Elapsed);
    }

    void tm_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
    System.Net.WebRequest rqst = System.Net.WebRequest.Create("the url of your website");
    rqst.GetResponse();
    }

    Please feel free to let me know if there is any problem.



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 26, 2007 2:41 AM
  • User-296603942 posted

    Steve and Raymond,

    Thanks for your replies.

    Steve - your idea was very unexpected but really neat. My problem is not the recompiling (sorry) but that the site gets recycled every now and then - otherwise I could just have hit the web site every time I make changes to it (which of course I do :-)  ). Your solution requires keeping a computer running 24/7, which I'd rather not do, but maybe I should anyway...

    Raymond - this was actually what I have been trying to find and work out for the last couple of days. Thank you so much! However, as I stated above, it'd really be best to set the timer to 21 minutes, so that periodically (say in the middle of the night when no one's accessing it) the web site gets recycled to get rid of memory hogs and so on. My guess is that setting the timer to 21 minutes would not work, as the application would die after only 20 minutes, so it would be kind of the boot-strapping/catch 22 analogy. Any comments on this would be most welcome.

    BTW, here's what Shados wrote me on this matter: "Honestly, let me give you an advice on this one. Application recycling/shutting down/restarting/etc is pretty much a "fact of life" in ASP.NET, especially in a shared host scenario, to reduce server load, and it should really be embraced, not worked against, a little like the browser's back button. The app starting might be a bit slow, but its not a large deal (even if your application is insanely huge, it will still be only a little startup). You "feel" it all the time as you browse the web daily, as ASP.NET isn't the only framework that does similar things. So just let it happen, only the most nitpicky user will ever worrie about it, and if they DO, they'll be worried about a LOT of web sites :) " But I would have to disagree with the conclusion. As long as there aren't that many users visiting the web site, most of them will experience this waiting time and it's really a shame, especially since I have tried rather hard to cache my sqldatasources, turned off enavbleviewstate, compressed my images carefully, and so forth.

    Pettrer

    Monday, March 26, 2007 4:39 AM
  • User-1573490007 posted
    For the record, the application will recycle on its own, no matter what, after it reaches a certain amount of memory. So you don't need to leave room for the occasional recycle, ASP.NET will -take- room, even if you're getting 100 hits/minutes :)
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 26, 2007 7:39 AM
  • User-1087479560 posted
    Hi Pettrer,

    I'd hold the same idea with Shados unless there is a specific reason you have to do so.
    Why you have to set the timer to 21 minutes?
    Monday, March 26, 2007 11:06 PM
  • User-296603942 posted

    @ Shados - Thanks for the clarification!

    @ Raymond - The 21 minutes would be there to assure that memory leaks or whatever that's needs to be taken care of every now and then actually gets taken care of. But Shados stated Asp.Net would take see to these matters anway, so I'll try the code you wrote.

    If everything works the way I want it to, I figure this would be one of the coolest features I've found out about in a long time!

    Pettrer

    Tuesday, March 27, 2007 3:53 AM
  • User-296603942 posted

    Hi again,

    This is just to confirm that the recycle solution WORKS LIKE A CHARM. After the web site hasn't been visited for 20 hours, it now took me <4 seconds to load the index page completely, instead of the usual 20 seconds. Boy, is there a difference. :-)

    Below is my VB version of the C# code posted above.

    Thanks again to all,

    Pettrer

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

    Dim tm As System.Timers.Timer = New System.Timers.Timer(19 * 60 * 1000)

    AddHandler tm.Elapsed, AddressOf tm_Elapsed

    End Sub

    Sub tm_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)

    Dim rqst As System.Net.WebRequest = System.Net.WebRequest.Create("http://mytempsite.se/Sokhjalp.aspx")

    rqst.GetResponse()

    End Sub

    Wednesday, March 28, 2007 3:13 AM
  • User722002358 posted

    Hi

    I have a similar problem (well kind of). I want to force my site to recompile even if there are users currently on it (and not after xxx minutes and no activity).

    If I make changes to my Web.config I want the changes to take affect immediately and not when the site recompliles eventually. I also don't have direct to IIS or any part of the OS since its hosted by a third party web host, so I'm guessing I need a solution to do this in code.

    Any ideas?

    ---------------------------------------

    Stefan Pienaar

    Wednesday, April 4, 2007 5:36 AM
  • User722002358 posted

    Never mind, ignore my last post please. Wasn't thinking at ALL.

     [:)]

     Cheers

    Stefan

    Wednesday, April 4, 2007 5:47 AM