記錄在Windows Server 2003 IIS6部署ASP.NET MVC 4專案的經驗。
部署第一步是複製檔案、設定Web Application、萬用字元應用程式對應等。(相關眉角可參考保哥的ASP.NET 4.0 安裝在 IIS6 最常遇到的四個問題)
第一枚茶包現身! 當初新增專案時一時不察,選了.NET Framework 4.5,web.config有兩行設定:
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
移到Windows 2003立即引爆。由於Windows 2003不支援.NET 4.5,只好乖乖地開Visual Studio將MVC專案降版為.NET 4.0。web.config內容被Visual Studio改成:
<compilation debug="true" targetFramework="4.0"/>
<httpRuntime/>
重新部署檔案,正期待看到網頁,另一枚茶包冒出來澆我一頭冷水:
[FileNotFoundException: Could not load file or assembly 'System.Net.Http, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.]
System.Web.Http.GlobalConfiguration..cctor() +0[TypeInitializationException: The type initializer for 'System.Web.Http.GlobalConfiguration' threw an exception.]
System.Web.Http.GlobalConfiguration.get_Configuration() +14
MyWeb.Areas.HelpPage.HelpPageAreaRegistration.RegisterArea(AreaRegistrationContext context) +94
System.Web.Mvc.AreaRegistration.CreateContextAndRegister(RouteCollection routes, Object state) +104
System.Web.Mvc.AreaRegistration.RegisterAllAreas(RouteCollection routes, IBuildManager buildManager, Object state) +195
System.Web.Mvc.AreaRegistration.RegisterAllAreas(Object state) +27
MyWeb.WebApiApplication.Application_Start() +12
推測是Visual Studio的4.5到4.0轉換不夠完整所致,決定另開一個.NET 4.0 MVC4專案比對最直接有效。很快找到一項明顯差異:
.NET 4.5轉.NET 4.0的MVC4專案,System.Net.Http.dll的參照來源是:
C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies\System.Net.Http.dll
而原生.NET 4.0 MVC4專案,System.Net.Http.dll則來自NuGet:
..\packages\Microsoft.Net.Http.2.0.20710.0\lib\net40\System.Net.Http.dll
由於System.Net.Http在.NET 4.5才納入BCL,猜想就是造成在.NET 4.0無法運作的元凶。問題專案先移除System.Net.Http參照,重新取用NuGet提供的版本,重新編譯、發佈後bin目錄多了System.Net.Http.dll與System.Net.Http.WebRequest.dll兩個檔案,再試一次,問題排除!!