Can't deploy precompiled, merged webapp to Azure

1.5k Views Asked by At

I'm trying to deploy an ASP.NET web application to Azure. It's hybrid Web Forms, MVC, and WebAPI, and there are a TON of aspx/ascx files, such that they really need to be precompiled or every deploy will render the site sluggish for awhile.

I am trying to deploy via SCM integration with GitHub via kudu, with precompiled views, all merged to a single assembly.

Note that:

  • Deploy works fine with precompilation disabled.
  • Deploy works fine from Visual Studio
  • Build works fine if I copy the msbuild command from the Azure log, replace the relevant paths, and run it locally on my Windows 8.1 machine.

I've set up the Advanced Precompile settings as:

  • Don't allow precompiled site to be udpatable
  • Don't emit debug information
  • Merge all pages and control outputs to a single assembly = AppViews.dll

Here's the .deployment file for Azure

[config]
project = WebSite/WebSite.csproj
SCM_BUILD_ARGS=/p:Configuration=Release;PublishProfile=azure-prod /v:n

You notice I'm sending the verbosity /v to "normal" for extra diagnostic information.

Here is info I get toward the tail of the deployment log:

AspNetPreCompile:
  D:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_compiler.exe -v \ -p D:\home\site\repository\WebSite\obj\Release\AspnetCompileMerge\Source -c D:\home\site\repository\WebSite\obj\Release\AspnetCompileMerge\TempBuildDir 
GenerateAssemblyInfoFromExistingAssembleInfo:
  Creating directory "obj\Release\AssemblyInfo".
  D:\Windows\Microsoft.NET\Framework\v4.0.30319\Csc.exe /out:obj\Release\AssemblyInfo\AssemblyInfo.dll /target:library Properties\AssemblyInfo.cs
AspNetMerge:
  Running aspnet_merge.exe.
  D:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\aspnet_merge.exe D:\home\site\repository\WebSite\obj\Release\AspnetCompileMerge\TempBuildDir -w AppViews.dll -copyattrs obj\Release\AssemblyInfo\AssemblyInfo.dll -a  
aspnet_merge : error 1003: The directory 'D:\home\site\repository\WebSite\obj\Release\AspnetCompileMerge\TempBuildDir' does not exist. [D:\home\site\repository\WebSite\WebSite.csproj]
Done Building Project "D:\home\site\repository\WebSite\WebSite.csproj" (Build;pipelinePreDeployCopyAllFilesToOneFolder target(s)) -- FAILED.

Build FAILED.

It looks like aspnet_compiler.exe runs, but doesn't do what it's supposed to, which is why the TempBuildDir directory (supposed to be the output of the compiler) does not exist in time for the AspNetMerge target. Contrast that with my system, where that directory DOES in fact exist, containing the marker aspx/ascx/etc. files, static content, a PrecompiledApp.config file, and a whole mess of stuff in the bin directory.

aspnet_compiler.exe has an -errorstack flag but it's not clear to me how I could get MSBuild to add this just via the .deployment file, or even if that app is really even throwing an error.

I could just deploy via Visual Studio, but I would really like to take advantage of the SCM integration so I can just push to my prod branch and let it go. Any suggestions?

2

There are 2 best solutions below

0
On BEST ANSWER

Note: This GitHub issue on projectkudu will eventually make this solution obsolete, but for the meantime, that issue is filed as Backlog, and this works right now.

Thank you thank you David Ebbo. With this information, I was able to bootstrap my build to work for the short term.

First, I downloaded the aspnet_compiler.exe from the Azure instance using the Diagnostic Console available at https://{WEBSITE_NAME}.scm.azurewebsites.net/DebugConsole and added that to my own repository. This way there's no question about any difference between 32/64-bit, etc. I renamed it to azure_aspnet_compiler.exe in my repository.

Second, the AspNetCompiler task doesn't give you the option to change the tool name. It's hardcoded, but as a virtual property so it's overrideable. So I had to create my own task class, and package it in its own assembly, which I built in Release mode and also included in my repository.

public class AzureAspNetCompiler : Microsoft.Build.Tasks.AspNetCompiler
{
    private string _toolName = "aspnet_compiler.exe";

    protected override string ToolName
    {
        get { return _toolName; }
    }

    public string CustomToolName // Because ToolName cannot have a setter
    {
        get { return _toolName; }
        set { _toolName = value; }
    }
}

Next I needed to replace the AspNetPreCompile task in MSBuild, but I couldn't figure out how to do that directly. But that task wasn't doing anything anyway, so why not just run right after it?

I added this to the top of my Website.csproj file to import the DLL containing the AzureAspNetCompiler class. Note that the path is relative to the Website.csproj file I'm editing.

<UsingTask TaskName="AzureBuildTargets.AzureAspNetCompiler"
           AssemblyFile="..\DeploymentTools\AzureBuildTargets.dll" />

Then I added this right below it, which is basically stealing the MSBuild target definition of AspNetPreCompile from C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\Web\Transform\Microsoft.Web.Publishing.AspNetCompileMerge.targets, with some of the property setting stuff near the top of it left out (because the original task will do that for us anyway.) Just take note of the ToolPath and CustomToolName values at the bottom of the (renamed) AzureAspNetCompiler element.

<PropertyGroup>
        <!--Relative to solution root apparently-->
        <LocalRepoDeploymentTools>.\DeploymentTools</LocalRepoDeploymentTools>
        <AzureAspnetCompilerPath>$([System.IO.Path]::GetFullPath($(LocalRepoDeploymentTools)))</AzureAspnetCompilerPath>
</PropertyGroup>

<Target Name="NoReallyAspNetPreCompile" AfterTargets="AspNetPreCompile">

<AzureAspNetCompiler
  PhysicalPath="$(_PreAspnetCompileMergeSingleTargetFolderFullPath)"
  TargetPath="$(_PostAspnetCompileMergeSingleTargetFolderFullPath)"
  VirtualPath="$(_AspNetCompilerVirtualPath)"
  Force="$(_AspNetCompilerForce)"
  Debug="$(DebugSymbols)"
  Updateable="$(EnableUpdateable)"
  KeyFile="$(_AspNetCompileMergeKeyFile)"
  KeyContainer="$(_AspNetCompileMergeKeyContainer)"
  DelaySign="$(DelaySign)"
  AllowPartiallyTrustedCallers="$(AllowPartiallyTrustedCallers)"
  FixedNames="$(_AspNetCompilerFixedNames)"
  Clean="$(Clean)"
  MetabasePath="$(_AspNetCompilerMetabasePath)"
  ToolPath="$(AzureAspnetCompilerPath)"
  CustomToolName="azure_aspnet_compiler.exe"
    />

<!--
    Removing APP_DATA is done here so that the output groups reflect the fact that App_data is
    not present
    -->
<RemoveDir Condition="'$(DeleteAppDataFolder)' == 'true' And Exists('$(_PostAspnetCompileMergeSingleTargetFolderFullPath)\App_Data')"
           Directories="$(_PostAspnetCompileMergeSingleTargetFolderFullPath)\App_Data" />


<CollectFilesinFolder Condition="'$(UseMerge)' != 'true'"
  RootPath="$(_PostAspnetCompileMergeSingleTargetFolderFullPath)" >
  <Output TaskParameter="Result" ItemName="_AspnetCompileMergePrecompiledOutputNoMetadata" />
</CollectFilesinFolder>

<ItemGroup Condition="'$(UseMerge)' != 'true'">
  <FileWrites Include="$(_PostAspnetCompileMergeSingleTargetFolderFullPath)\**"/>
</ItemGroup>

With this in place, everything works as I would expect it to.

0
On

I replied on https://github.com/projectkudu/kudu/issues/1341, but copying my answer here in case someone lands here...

Way back, we had found that aspnet_compiler.exe was not working within Azure Websites due to how it dealt with the profile folder. We made a change at the time that's a bit of a hack but got us going: we turned it into a no-op, by pointing HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\aspnet_compiler.exe to our own dummy exe (D:\Program Files (x86)\aspnet_compiler\KuduAspNetCompiler.exe).

But trying it now, it appears to work correctly today, likely thanks to improvements in the Azure Websites hosting environment. So we will try getting rid of this hack and doing a full test pass to make sure it doesn't cause any major regressions. If all goes well, we can get that into production, which should enable those scenarios.

In the short term, you may be able to work around this by having your build script:

  • copy aspnet_compiler.exe from D:\Windows\Microsoft.NET\Framework\v4.0.30319 into your own site files, but under a different name (e.g. aspnet_compiler2.exe)
  • convince msbuild to use that one