26

Is there a way to precompile the ASP.NET MVC views on an Azure Web App (specifically when published via Release Management on VSTS)?

Once each view has been hit once, the page subsequently renders very quickly. But that first delay can be a doozy for users and there's no way to script touching each page.

I'm not sure if I need to change something in the build/release processes on VSTS (I am using the Visual Studio Build build step and the Azure Web App release task) or if I need to run something on the Azure Web App instance after it is released (or something else altogether).

It seems like finding some way to call aspnet_compiler after publish might be what I need (and I have seen that in reference to Web Roles on Cloud Services) but I can't get that to work.

Calling

%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_compiler -v "/" -p "d:\home\site\wwwroot" 

via the Console in the Azure Portal executes just fine (and finds errors if there are any) but doesn't have any impact on startup time - hitting a view the first time still takes a long time.

So maybe that isn't the right direction.

I've looked at RazorGenerator (including the .MSBuild nuget package) and I couldn't quite get it to work, but really I was hesitant to make so many changes to the projects just to get precompilation on release.

Also note that I am currently using TFVC, not Git, in VSTS, so the Kudu/Git integration (that does seem to trigger the precompilation according to some articles) isn't available to me as far as I can tell.

Other ideas?

Tim
  • 14,999
  • 1
  • 45
  • 68
  • I wonder if running a cloud based web performance test on the site after your release task will make the first slow hit on your views? – majita May 13 '16 at 06:39

2 Answers2

40

You can add "/p:PrecompileBeforePublish=true" argument in "Visual Studio Build" step: enter image description here

Then the task will call aspnet_compiler during the build process and generate a precompiled output for deployment.

Eddie Chen - MSFT
  • 29,708
  • 2
  • 46
  • 60
  • 2
    Is it possible to use `PrecompileBeforePublish` argument when Azure App Service is published directly via `msdeploy.exe` call? – Ilya Chumakov May 13 '16 at 06:57
  • Interesting. I had been told that didn't work. But it clearly does. Well, awesome. Thanks! – Tim May 13 '16 at 14:40
  • 14
    This will create a dll per view. If you also add /p:UseMerge=true /p:SingleAssemblyName=AppCode to the argument you will only have a single dll, which is faster to load. – David De Sloovere Jan 08 '17 at 11:58
  • I'm trying it, but in Azure DevOps I get the following error: ##[error]Project\obj\release\aspnetcompilemerge\source\web.config(52,0): Error ASPCONFIG: It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS. – Albert Nov 06 '19 at 17:16
  • @Albert Hi, What did you do to solve this issue? I am getting the same issue as you are. – Omkar Nov 18 '19 at 11:13
  • @Omkar Solved by specifying another path for obj folder using '/p:BaseIntermediateOutputPath=$(build.artifactstagingdirectory)\tmp_obj\' argument – Albert Nov 18 '19 at 15:32
2

In visual studio 2013+, choose Build->Profiles. Select the profile, then choose "Settings". Underneath "Configuration", expand "File Publish Options", and then check the "Precompile during publishing" option.

1c1cle
  • 433
  • 3
  • 6