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

Viewed 724

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.

2 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();
        }
    

Asp.net uses dynamic compilation by default (https://docs.microsoft.com/en-us/previous-versions/bb398860(v=vs.140)?redirectedfrom=MSDN#default-compilation).

You can deploy your site by performing precompilation (https://docs.microsoft.com/en-us/previous-versions/bb398860(v=vs.140)?redirectedfrom=MSDN#performing-precompilation). Deploy your site from visual studio's publish web site tool.

Or you can just enable optimizeCompilations attribute in compilation element in web.config. If its true, asp.net will just compile the modified file, not your whole site.

<compilation targetFramework="4.7.2" optimizeCompilations="true" />
Related