6

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.

done_merson
  • 2,800
  • 2
  • 22
  • 30
  • 2
    It is recommended to involve application initalization and IIS always running. https://weblog.west-wind.com/posts/2013/oct/02/use-iis-application-initialization-for-keeping-aspnet-apps-alive – Jokies Ding May 18 '20 at 08:00
  • "when I copy my site to the web hosting computer." - this quote right here makes me curious as to **How** you are deploying your site. Simply copying your solution directory over should be avoided at all costs if that is how you are deploying and could explain some of the recompilation issues you are seeing. Better options are using the Visual Studio publish feature and choosing Web Deploy or FTP. Best practice is to use a CI/CD tool like Azure DevOps to manage deploying of your application (recognizing this might be overkill for your situation however) – Tommy Jun 06 '20 at 14:25
  • It is being deploy via XCOPY not the files in the solution. – done_merson Jun 12 '20 at 20:30

2 Answers2

4
  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();
        }
    
Ashiquzzaman
  • 5,129
  • 3
  • 27
  • 38
0

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

You can deploy your site by performing precompilation (https://learn.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" />