Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Precompile ASP.NET MVC Project in Azure (not with Web Deploy)

I would like to precompile an ASP.NET MVC 4 site, but within an Azure Web Role. It's particularly nice how this catches cshtml view errors.

This is easily done with Web Deploy, by just checking a couple of appropriate boxes in the Publish -> File Publish Options.

But when you have more than 1 instance of a WebRole, you won't be using Web Deploy. And I don't see any options within the Project settings (in the Build or Package/Publish Web tabs) to specify these precompilation build options.

Is there a way I can specify precompile options at the project level somehow? I'm not keen either on any options that would require having to leave the workflow of being able to publish to Azure with the simple (but slow) "Publish to Windows Azure" option (i.e. I am not interested in having to somehow manually upload the azure cloud package, not at all).

like image 660
Nicholas Petersen Avatar asked Mar 05 '13 19:03

Nicholas Petersen


1 Answers

The following may not be everything one desires, and I wish the Visual Studio team could add a simple UI project settings toggle to do the same (which might be a very trivial task), but here is a way I have found since asking this question to get the cshtml razor files compiled. Unfortunately though it happens on every build, which really is a lot slower, but if you set this on Release only, then it is a good compromise:

Go to your project folder (right click project in VS, -> Open Folder...), locate the .csproj file, open it in notepad (++, of course), then find a few PropertyGroups down in the root of the xml ('Project') a PropertyGroup with this condition, which specifies settings specific to Release mode only (there is another for Debug, which you can do this same thing on):

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <MvcBuildViews>true</MvcBuildViews>
    <NoWarn>162, 168, 169, 219, 414, 612, 618, 649, 1591</NoWarn>
  </PropertyGroup>

Simply add the MvcBuildViews element (or alter from false if it already exists) and make sure the value is true, and suddenly, you'll notice every build takes a good deal longer [insert smiley wink emoticon here]. But you do get compilation of your razor code, it will catch errors like you wanted it to do, very nice. So the team already has this baked in, odd they don't add the UI option to MVC project settings.

I guess this answers my original question: "Is there a way I can specify precompile options at the project level somehow?" I found the answer I think somewhere online, I don't remember where now, and I'm sure it's on SO somewhere else as well. Thanks to David for shedding light on this question again.

like image 166
Nicholas Petersen Avatar answered Nov 02 '22 10:11

Nicholas Petersen