Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Tip: Slow ASP.NET MVC Start-up Has Users Waiting On Your Web Site

0.00/5 (No votes)
13 Aug 2016 2  
My ASP.NET MVC 5 web site is simple but gets small amount of traffic so after 20 minutes, it shuts down. That means new visitors wait 20 seconds or more to see my landing page. Here's a way to test and see if that is your problem.

Introduction

My web site (http://raddev.us[^]) is very simple and based upon ASP.NET MVC 5 Visual Studio template. It's hosted on GoDaddy.com (simply because it is a cheap yet relatively effective web host).

I've been wondering why my web site causes the browser to spin and display a message in Chrome that says, "waiting on raddev.us...".

Background

I searched the web and stumbled upon numerous ideas that are often based upon having complete control of IIS and the machine config file. Here's an example article which provides a lot of answers and solutions which mostly won't work for us who are on shared IIS servers which are found at most web hosting services:
ASP.NET - Fixing slow initial load for IIS - Stack Overflow[^]

Determining If the Problem is ASP.NET Startup

The main thing I wanted to do is determine if the problem was related to ASP.NET startup. My assumption was that this was the problem.

Three Reasons I Believed the Problem was ASP.NET Startup

  1. I'd previously written script to test DNS lookup time latency and those compared favorably to other sites.
  2. If I load a straight HTML page (no ASP.NET engine involved), then the page loads extremely fast.
  3. Even the most simple ASP.NET MVC landing page is extremely slow the first time and then loads more quickly afterward -- until the web app unloads, then it is slow again.

Looking For A More Definitive Answer

I was looking for a more definitive answer so I decided to write the following script which:

  1. runs every 8 minutes (every 20 minutes the ASP.NET MVC app will unload if no activity)
  2. hits my main page and retrieves the data

Using LinqPad

I run this script from LinqPad. If you haven't heard of the utility, check out the free utility : LINQPad - The .NET Programmer's Playground[^]

Once you have LinqPad, copy the following script in and set the URL to the URL that you want to load every 8 minutes.

System.Timers.Timer webKeepAlive = new System.Timers.Timer();
Int64 counter = 0;
void Main()
{
    webKeepAlive.Interval = 5000;
    webKeepAlive.Elapsed += WebKeepAlive_Elapsed;
    webKeepAlive.Start();
}

private void WebKeepAlive_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
      webKeepAlive.Stop();
    try
    {
        String finalHtml = GetWebContent();
        if (counter < 1)
        {
            Console.WriteLine(finalHtml);
        }
        counter++;
    }
    finally
    {
        webKeepAlive.Interval = 480000; // every 8 minutes
        webKeepAlive.Start();
    }
}

public String GetWebContent()
{
    try
    {
    string URL = "SET YOUR URL HERE";
    WebRequest request = WebRequest.Create(URL);
    WebResponse response = request.GetResponse();
    Stream data = response.GetResponseStream();
    string html = String.Empty;
    using (StreamReader sr = new StreamReader(data))
    {
        html = sr.ReadToEnd();
    }
    Console.WriteLine (String.Format("{0} : success",DateTime.Now));
    return html;
    }
    catch (Exception ex)
    {
        Console.WriteLine (String.Format("{0} -- GetWebContent() : {1}",DateTime.Now,ex.Message));
        return "fail";
    }
}

Or, you can simply download the keepAlive.zip, unzip it and open it from LinqPad.

After that, just change thestring URL = "SET YOUR URL HERE" and set the string to point to your web site and run the script.

Script Creates Timer and Keeps Running

The first time the timer will fire after 5 seconds and retrieve your web page.

It will write the entire web page to the console at the bottom of LinqPad just so you can see that it's retrieving something. After that, it will just display a line showing that it has successfully retrieved the page.

<img src="1118195/keepAlive.png" style="width: 544px; height: 278px;" />

Now, Test Your Web Site

If your web site loads extremely fast throughout the day while this script is running, it is probably a good indicator (along with the other data points I provided) that the slowness is from the ASP.NET MVC engine startup.

My site loads extremely fast while this script is running, because the app is always loaded.

I have left the LinqPad script running for 48 hours straight on my computer.

Stopping the Script Without Closing LinqPad

The Timer runs on a separate thread from the LinqPad UI so if you want to stop the script without closing LinqPad, you simply press Ctrl-Shift-F5.

Complete Solution

Now I'm looking into how to keep my web app alive at all times so visitors don't experience the lag.

History

  • 2016-08-13 - Initial release of article and code

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here