Click here to Skip to main content
15,887,267 members
Articles / Web Development
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.22/5 (6 votes)
13 Aug 2016CPOL3 min read 23.7K   86   3   6
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.

C#
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 the string 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.

Image 1

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) RADDev Publishing
United States United States
"Everything should be made as simple as possible, but not simpler."

Comments and Discussions

 
GeneralLook at Idle Timeout on app pool Pin
Middle Manager15-Aug-16 7:29
Middle Manager15-Aug-16 7:29 
GeneralRe: Look at Idle Timeout on app pool Pin
raddevus15-Aug-16 9:53
mvaraddevus15-Aug-16 9:53 
GeneralRe: Look at Idle Timeout on app pool Pin
yxhu17-Aug-16 14:36
yxhu17-Aug-16 14:36 
GeneralRe: Look at Idle Timeout on app pool Pin
raddevus17-Aug-16 18:19
mvaraddevus17-Aug-16 18:19 
GeneralRe: Look at Idle Timeout on app pool Pin
yxhu17-Aug-16 21:23
yxhu17-Aug-16 21:23 
GeneralRe: Look at Idle Timeout on app pool Pin
raddevus18-Aug-16 2:25
mvaraddevus18-Aug-16 2:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.