同事回報網站部署到測試主機 IIS 遇到詭異錯誤:

Server Error in '/MyApp' Application.

Configuration Error 
  Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. 

 Parser Error Message: The CodeDom provider type "Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" could not be located.

Source Error: 

Line 93:       <system.codedom>
Line 94:         <compilers>
Line 95:           <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
Line 96:           <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
Line 97:         </compilers>

進行了解,發現狀況有點複雜:本次部署的網站應用程式為 Web Site Project,在 IIS 掛成子網站,而根站台是 ASP.NET MVC。登楞,MVC 下的子網站是 Web Site Project?這種罕見組合應不在微軟研發團隊原本設想的情境裡,爬文也找不到什麼相似案例,卻是古蹟維護工作人員的日常。

由訊息研判,是 MVC web.config 裡的部分設定繼承到 Web Site Project 子網站,而古老 WSP 的編譯機制認不得來屬於未來的 ASP.NET MVC 設定,無法相容造成錯誤,切斷繼承關係應可解決問題。

使用古老的 inheritInChildApplication 技巧將 <system.codedom> 包起來並指定 inheritInChildApplications="false":

<location path="." inheritInChildApplications="false">
     <system.codedom>
       <compilers>
         <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
         <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
       </compilers>
   </system.codedom>
 </location>

原本錯誤消失,再冒出新錯誤,這回則是抱怨找不到 System.Web.Helpers:

Server Error in '/MyApp' Application.

Compilation Error 
  Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

 Compiler Error Message: CS0234: The type or namespace name 'Helpers' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)

Source Error:

 

Line 32:         <pages>
Line 33:           <namespaces>
Line 34:             <add namespace="System.Web.Helpers" />
Line 35:             <add namespace="System.Web.Mvc" />
Line 36:             <add namespace="System.Web.Mvc.Ajax" />

再使出 inheritInChildApplications 大法將整個 <system.codedom> 也包起來:

  <location path="." inheritInChildApplications="false" >
      <system.web>
        <compilation debug="true" targetFramework="4.6.1" />
        <httpRuntime targetFramework="4.6.1" />
        <pages>
          <namespaces>
            <add namespace="System.Web.Helpers" />
            <add namespace="System.Web.Mvc" />
            <add namespace="System.Web.Mvc.Ajax" />
            <add namespace="System.Web.Mvc.Html" />
            <add namespace="System.Web.Routing" />
            <add namespace="System.Web.WebPages" />
          </namespaces>
        </pages>
      </system.web>
  </location>

幸運地,做完以上修正,問題排除。以上經驗與古蹟維護同業們分享。

Experience of making web site project running as sub web application under a ASP.NET MVC root web application in IIS.


Comments

# by kb

厉害

Post a comment


99 - 32 =