I have a Windows Azure web role that contains a web site using ASP.NET MVC. When an HTTP request arrives and a page is first loaded the view (.aspx or .cshtml) is compiled and that takes some time and so the first time a page is served it takes notable longer than later serving the same page.
I’ve enabled <MvcBuildViews> (described in this answer) to enforce compile-time validation of views, but that doesn’t seem to have any effect on their compilation when the site is deployed and running.
Azure web roles have so-called startup tasks and also a special OnStart() method where I can place whatever warmup code, so once I know what to do adding that into the role is not a problem.
Is there a way to force compilation of all views?
Turns out there’s ASP.NET Precompilation that can be performed using ClientBuildManager.PrecompileApplication and mimics the on-demand compilation behavior, but just compiles every page. Tried it – the first load looks notably faster.
The non-trivial part is what to pass as
ClientBuildManagerconstructor parameters. The solution is to enumerate all.Applicationsof theSiteobject and for each item in.Applicationsenumerate all.VirtualDirectoriesand usePathandVirtualPathfrom each item as parameters toClientBuildManagerconstructor.