Asp.net
ASP.NET MVC 1.0 AfterBuilding 視圖在 TFS 建構上失敗
我已從 ASP.NET MVC Beta 升級到 1.0,並對 MVC 項目進行了以下更改(如 RC 發行說明中所述):
<Project ...> ... <MvcBuildViews>true</MvcBuildViews> ... <Target Name="AfterBuild" Condition="'$(MvcBuildViews)'=='true'"> <AspNetCompiler VirtualPath="temp" PhysicalPath="$(ProjectDir)\..\$(ProjectName)" /> </Target> ... </Project>雖然建構在我們的本地開發盒上執行良好,但它在 TFS 2008 建構下失敗,出現“無法載入類型 ‘xxx.MvcApplication’”,請參見下面的建構日誌:
... using "AspNetCompiler" task from assembly "Microsoft.Build.Tasks.v3.5, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a". Task "AspNetCompiler" Command: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_compiler.exe -v temp -p D:\Builds\xxx\Continuous\TeamBuild\Sources\UI\xxx.UI.Dashboard\\..\xxx.UI.Dashboard The "AspNetCompiler" task is using "aspnet_compiler.exe" from "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_compiler.exe". Utility to precompile an ASP.NET application Copyright (C) Microsoft Corporation. All rights reserved. /temp/global.asax(1): error ASPPARSE: Could not load type 'xxx.UI.Dashboard.MvcApplication'. The command exited with code 1. Done executing task "AspNetCompiler" -- FAILED. ...MVC 1.0 安裝在 TFS 上,解決方案在同一 TFS 伺服器上的 Visual Studio 實例中建構時編譯。
如何解決此 TFS 建構問題?
問題源於這樣一個事實,即在 ASP.NET MVC 項目的 AfterBuild 目標中使用的 AspNetCompiler MSBuild 任務期望引用 Web 項目的 bin 文件夾中的 dll。
在桌面建構中,bin 文件夾是您在原始碼樹下所期望的位置。
但是,TFS Teambuild 會將原始碼的輸出編譯到建構伺服器上的不同目錄。當 AspNetCompiler 任務啟動時,它找不到 bin 目錄來引用所需的 DLL,並且您會收到異常。
解決方法是將MVC項目的AfterBuild目標修改為如下:
<Target Name="AfterBuild" Condition="'$(MvcBuildViews)'=='true'"> <AspNetCompiler Condition="'$(IsDesktopBuild)' != 'false'" VirtualPath="temp" PhysicalPath="$(ProjectDir)\..\$(ProjectName)" /> <AspNetCompiler Condition="'$(IsDesktopBuild)' == 'false'" VirtualPath="temp" PhysicalPath="$(PublishDir)\_PublishedWebsites\$(ProjectName)" /> </Target>此更改使您能夠在桌面和 TFS 建構伺服器上編譯視圖。
實際上,這個問題有更好的解決方案。我已經使用 VS/TFS 2010 對其進行了測試,但它也應該適用於 VS/TFS 2008。
<Target Name="AfterBuild" Condition="'$(MvcBuildViews)'=='true'"> <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" /> </Target>我將與 MVC 團隊合作更新他們的項目模板以使用這種方法以及自定義目標(而不是覆蓋 AfterBuild)。
我發表了一篇關於如何在 TFS Build 2010 中為 ASP.NET MVC 項目打開編譯時視圖檢查的部落格文章。