Keep Your Sitefinity Website Alive !

Posted by Community Admin on 04-Aug-2018 07:30

Keep Your Sitefinity Website Alive !

All Replies

Posted by Community Admin on 16-Nov-2016 00:00

Hi All,

I have tried to find a simple method to keep my website alive, thus never have to see the sitefinity startup screen as soon as the number of requests to the website goes down.

The code I found has been tested and works quite well in my local development environment.

If we place this bit of code in the Global.asax and call it when Application_Start() is raised, we can basically start a job that keeps our website alive. You could just as easily use a Thread to host the refresh method but for this example we simply used an Action delegate.

Once our application starts the refresh job is also started and is saved to the cache. In this example we’re using 60 seconds, but you can change this to be as often as you like.
This snippet uses a WebClient to actually make an HTTP call to our website, thus keeping the site alive! We could do any number of things from this code like updating local data or get information from external resource. This can be used to keep our site alive and our content refreshed, even if we’re using a Hosted Environment!

It is worth nothing that might not actually need to do an HTTP call back to your website. It is possible that using any method will keep your website from being killed off (but I haven’t tested it yet so let me know what happens if you try it).

I have not noticed any interference with Sitefinity so far, and would be surprised if problems occur.

public class Global : HttpApplication

    private static readonly log4net.ILog GlobalLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    protected void Application_Start(object sender, EventArgs e)
   
        //  Configure Log4Net
        log4net.Config.XmlConfigurator.Configure();
        //  Keep our website warm
        _SetupRefreshJob();
   

    /// <summary>
    /// Keep our website refreshed and warm for fast response even during low load.
    /// NOTE:   This code was copied from an article on codeproject.
    ///         www.codeproject.com/.../Keep-Your-Website-Alive-Don-t-Let-IIS-Recycle-Your
    ///
    /// </summary>
    private static void _SetupRefreshJob()
   
        //  Remove a previous job from the cache
        Action remove = HttpContext.Current.Cache["Refresh"] as Action;
        if (remove is Action)
       
            HttpContext.Current.Cache.Remove("Refresh");
            remove.EndInvoke(null);
       
        //  Get the worker
        Action work = () =>
            while (true)
           
                //  Set time interval to 60 seconds.
                Thread.Sleep(60000);
                WebClient refresh = new WebClient();
                try
               
                    refresh.UploadString("http://www.domainname.com/", string.Empty);
               
                catch (Exception ex)
               
                    GlobalLog.Error(ex);
               
                finally
               
                    refresh.Dispose();
               
           
        ;
        work.BeginInvoke(null, null);
        //  Add this job to the cache.
        HttpContext.Current.Cache.Add(
            "Refresh",
            work,
            null,
            Cache.NoAbsoluteExpiration,
            Cache.NoSlidingExpiration,
            CacheItemPriority.Normal,
            (s, o, r) => _SetupRefreshJob();
            );
   

Rgds/Gunnar

This thread is closed