Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initial Loads on ASP.NET MVC Site for every file are slow

I have an ASP.NET MVC website that I have verified that is compiling each time a new C# file (like a controller) is hit for the first time. I have looked at the Task Manager and every first time a new controller is hit, the page is slow, the CPU gets peaked because of the compiler.

I had the Rosyln compiler before but I have switched back the regular C# compiler without any change.

I have tried to precompile but it doesn't seem to matter when I copy my site to the web hosting computer.

I don't remember this happening on the previous version of apps that I worked with but most of those were mostly ASP.NET Forms with MVC throw into the mix.

Is this normal behavior or is this something I can rectify with a setting? I want it to compile all files when the site is first deployed. (In fact, it is so long for the first page I am not sure how it isn't doing this)

Right now, I have a script that hits every controller after I deploy my application which keeps the issue at bay.

To duplicate, just copy a new main dll to your bin folder. Then look at your task manager as you browse to different pages with different controllers.

like image 929
done_merson Avatar asked May 15 '20 23:05

done_merson


People also ask

Why are ASP Net Applications slightly slower on first load?

Typically an application will always take a little extra time to load as the application domain starts up. Things helping exacerbate this could be anything from poorly written code (IE: Application_Start) to certain libraries you may be using (ORMs for example). How many modules do you have loaded?

Why MVC is faster than ASP Net?

MVC provides better support to TDD (Test driven development). TDD is related to the test first programming concepts of extreme programming. It helps us to reduced time in reworks and helps create loosely coupled code. MVC enforces separation that reduces complexity of project structure.


1 Answers

  1. You can improve your application preference using caching. Please read this article.

  2. Also you can involve application initialization and IIS always running. For this reason you need set startMode of your application pool to always running, this will prevent your application pool from sleeping, after some time.

    For more information please read below posts:

    1. iis-80-application-initialization

    2. use-iis-application-initialization-for-keeping-aspnet-apps-alive

    3. you can always pin you application for keeping alive (N:B:- IT's not great idea if you're running in the cloud. But in shared hosting or dedicated server is working fine (tested)).

    Example:

    public class RecycleConfig
    {
        public static void PingSite()
        {
            using (var refresh = new WebClient())
            {
                try
                {
                    refresh.DownloadString("http://yoursote.com");
                }
                catch (Exception)
                {
    
                }
            }
        }
    
        public static void SetupRefreshJob()
        {
            if (HttpContext.Current == null) return;
    
            //remove a previous job
            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)
                {
                    Thread.Sleep(600000);
                    PingSite();
                }
            };
            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(); }
              );
        }
    }
    

    in Global.asax add --

        protected void Application_Start()
        {
    
            RecycleConfig.SetupRefreshJob();
            /........
            /........
        }
    
        protected void Application_End(object sender, EventArgs e)
        {
            RecycleConfig.PingSite();
        }
    
like image 95
Ashiquzzaman Avatar answered Oct 21 '22 21:10

Ashiquzzaman