Asp.net

無法載入文件或程序集“System.Web.Mvc”。如何使用正確的參考?

  • September 19, 2015

我有一個基於 ASP.NET Web 表單的應用程序,它也支持 MVC 5。我在項目中包含了 MVC 5 API 控制器,為此我必須將 Json 從 4.5 版升級到 7.0。在啟動應用程序時進行這些更改後,我收到以下錯誤:

無法載入文件或程序集“System.Web.Mvc”或其依賴項之一。找到的程序集的清單定義與程序集引用不匹配。(來自 HRESULT 的異常:0x80131040)

我檢查了System.Web.Mvc. 在屬性對話框執行時版本是v4.0.30319和版本是5.0.0.0。在 web.config 中,程序集設置如下:

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
 <dependentAssembly>
   <assemblyIdentity name="NLog" publicKeyToken="5120e14c03d0593c" culture="neutral" />
   <bindingRedirect oldVersion="0.0.0.0-2.0.1.0" newVersion="2.0.1.0" />
 </dependentAssembly>
 <dependentAssembly>
   <assemblyIdentity name="AntiXssLibrary" publicKeyToken="d127efab8a9c114f" culture="neutral" />
   <bindingRedirect oldVersion="0.0.0.0-4.3.0.0" newVersion="4.3.0.0" />
 </dependentAssembly>
 <dependentAssembly>
   <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
   <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
 </dependentAssembly>
 <dependentAssembly>
   <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
   <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
 </dependentAssembly>
 <dependentAssembly>
   <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
   <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
 </dependentAssembly>
 <!--<dependentAssembly>
   <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
   <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0" />
 </dependentAssembly>-->
 <dependentAssembly>
   <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
   <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
 </dependentAssembly>
 <dependentAssembly>
   <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
   <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
 </dependentAssembly>
 <dependentAssembly>
   <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
   <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
 </dependentAssembly>
</assemblyBinding>

完整的錯誤日誌如下所示:

=== 預綁定狀態資訊 === LOG:DisplayName = System.Web.Mvc(部分) WRN:為程序集提供了部分綁定資訊:WRN:程序集名稱:System.Web.Mvc | 域 ID:2 警告:僅提供部分程序集顯示名稱時會發生部分綁定。警告:這可能會導致活頁夾載入不正確的程序集。WRN:建議為程序集提供完全指定的文本標識,WRN:由簡單名稱、版本、文化和公鑰標記組成。警告:請參閱白皮書 <http://go.microsoft.com/fwlink/?LinkId=109270>有關此問題的更多資訊和常見解決方案。LOG: Appbase = file:///C:/Users/Ahmed/Documents/Visual Studio 2013/Projects/MediaSoft/Project/c#/YuClone/YuClone/ LOG: Initial PrivatePath = C:\Users\Ahmed\Documents\Visual Studio 2013\Projects\MediaSoft\Project\c#\YuClone\YuClone\bin 呼叫程序集:(未知)。=== LOG:此綁定在預設載入上下文中開始。LOG:使用應用程序配置文件:C:\Users\Ahmed\Documents\Visual Studio 2013\Projects\MediaSoft\Project\c#\YuClone\YuClone\web.config LOG:使用主機配置文件:C:\Users\Ahmed\Documents \IISExpress\config\aspnet.config LOG:使用 C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config 中的機器配置文件。日誌:此時未將策略應用於參考(私有、自定義、部分、或基於位置的程序集綁定)。日誌:嘗試下載新的 URL 文件:///C:/Users/Ahmed/AppData/Local/Temp/Temporary ASP.NET Files/root/22a49c5f/2f5ffeb9/System.Web.Mvc.DLL。日誌:嘗試下載新的 URL 文件:///C:/Users/Ahmed/AppData/Local/Temp/Temporary ASP.NET Files/root/22a49c5f/2f5ffeb9/System.Web.Mvc/System.Web.Mvc.DLL . 日誌:正在嘗試下載新的 URL 文件:///C:/Users/Ahmed/Documents/Visual Studio 2013/Projects/MediaSoft/Project/c#/YuClone/YuClone/bin/System.Web.Mvc.DLL。LOG:使用應用程序配置文件:C:\Users\Ahmed\Documents\Visual Studio 2013\Projects\MediaSoft\Project\c#\YuClone\YuClone\web.config LOG:使用主機配置文件:C:\Users\Ahmed\Documents \IISExpress\config\aspnet.config LOG:使用 C:\Windows\Microsoft.NET\Framework\v4.0 中的機器配置文件。30319\config\machine.config。LOG:在應用程序配置文件中找到重定向:5.0.0.0 重定向到 5.2.3.0。日誌:策略後參考:System.Web.Mvc,版本=5.2.3.0,文化=中性,PublicKeyToken=31bf3856ad364e35 日誌:嘗試下載新 URL 文件:///C:/Users/Ahmed/AppData/Local/Temp /臨時 ASP.NET 文件/root/22a49c5f/2f5ffeb9/System.Web.Mvc.DLL。日誌:嘗試下載新的 URL 文件:///C:/Users/Ahmed/AppData/Local/Temp/Temporary ASP.NET Files/root/22a49c5f/2f5ffeb9/System.Web.Mvc/System.Web.Mvc.DLL . 日誌:正在嘗試下載新的 URL 文件:///C:/Users/Ahmed/Documents/Visual Studio 2013/Projects/MediaSoft/Project/c#/YuClone/YuClone/bin/System.Web.Mvc.DLL。警告:比較程序集名稱導致不匹配:次要版本錯誤:無法完成程序集設置(hr = 0x80131040)。

以下是堆棧跟踪

[FileLoadException:無法載入文件或程序集“System.Web.Mvc”或其依賴項之一。找到的程序集的清單定義與程序集引用不匹配。(來自 HRESULT 的異常:0x80131040)]

[FileLoadException:無法載入文件或程序集“System.Web.Mvc,版本=5.2.3.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35”或其依賴項之一。找到的程序集的清單定義與程序集引用不匹配。(HRESULT 異常:0x80131040)] System.Reflection.RuntimeAssembly._nLoad(AssemblyName 文件名,字元串 codeBase,證據 assemblySecurity,RuntimeAssembly locationHint,StackCrawlMark 和 stackMark,IntPtr pPrivHostBinder,布爾 throwOnFileNotFound,布爾 forIntrospection,布爾suppressSecurityChecks)+0

System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark & stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +36

System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assembly , RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean for Introspection, Boolean suppressSecurityChecks) +152

System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection) +77

System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean for Introspection) +21 System.Reflection.Assembly.Load(String assemblyString) +28

System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective ) +38

[ConfigurationErrorsException:無法載入文件或程序集“System.Web.Mvc,版本=5.2.3.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35”或其依賴項之一。找到的程序集的清單定義與程序集引用不匹配。(HRESULT 異常:0x80131040)]

System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +738

System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +217 System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +130

System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +170

System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies() +92 System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +290

System.Web.Compilation.BuildManager.ExecutePreAppStart() +157

System.Web.Hosting。 HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +531

[HttpException (0x80004005): 無法載入文件或程序集“System.Web.Mvc,版本=5.2.3.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35”或其依賴項之一。找到的程序集的清單定義與程序集引用不匹配。(HRESULT 異常:0x80131040)]

System.Web.HttpRuntime.FirstRequestInit(HttpContext 上下文)+9924184 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext 上下文)+101 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr,HttpContext 上下文)+261

如何升級 System.Web.Mvc 的版本以指向正確的程序集?

錯誤是由於不正確的綁定引起的。我變了:

&lt;dependentAssembly&gt;
   &lt;assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" /&gt;
   &lt;bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" /&gt;
 &lt;/dependentAssembly&gt;

 &lt;dependentAssembly&gt;
&lt;assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" /&gt;
&lt;bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.0.0.0" /&gt;

現在可以了。

引用自:https://stackoverflow.com/questions/32668590