.Net 4.6 网站未正确加载参考程序集

.Net 4.6 website not loading Reference Assemblies correctly

我在 Visual Studio 2013 年有一个网络项目,包括几个图书馆项目。

问题在于,加载到 Web 项目时,添加引用(即 System.Collection、System.Net)会作为来自 C:\Program Files (x86)\Reference Assemblies\Microsoft 的 'Reference Assembly' 添加IIS 未正确加载程序集的执行(来自 GAC)。示例错误如下。

[BadImageFormatException: Cannot load a reference assembly for execution.]

[BadImageFormatException: Could not load file or assembly 'System.Collections' or one of its dependencies. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)]
   System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0
   System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +34
   System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, 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 forIntrospection) +16
   System.Reflection.Assembly.Load(String assemblyString) +28
   System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +38

[ConfigurationErrorsException: Could not load file or assembly 'System.Collections' or one of its dependencies. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)]
   System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +728
   System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +196
   System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +45
   System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +172
   System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies() +91
   System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +111
   System.Web.Compilation.BuildManager.ExecutePreAppStart() +156
   System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +624

[HttpException (0x80004005): Could not load file or assembly 'System.Collections' or one of its dependencies. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +659
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +95
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +189

从 bin 文件夹中删除引用 dll 可以解决此问题,但我不确定需要更改哪些内容才能正确解决此问题。

只要您看到 BadImageFormatException,您就会遇到二进制格式兼容性问题。可能是您的 IIS 池配置为 运行 32 位池并且您的程序集构建为 x64,或者相反。或者,可能是您尝试在 32 位机器上 运行 x64 程序集。可能你有 x64 机器,任何 cpu 构建的程序集,但一些第 3 方程序集严格按照 32 位代码构建。

是其中之一,或类似的

现在,"usr" 说得有道理。您确实有 Cannot load a reference assembly for execution,但在 BadImageFormatException 的上下文中。我想知道这是否发生在编译时。为此,尝试将其添加到 web.config

<compilation>
  <assemblies>
      <remove assembly="System.Collections" />
  . . . . 

或者,如果您有

<add assembly="System.Collections. . . ."  />

先尝试删除它

现在,除非您提供 probing settings

,否则 GAC 是参考的首选位置是正常的

解析:

我的库项目引用了一些核心库(System.* 等),RequiredTargetFramework 选项设置为 3.5。这仅在 csproj 文件中很明显,例如:

<Reference Include="System.Core">
  <RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>

因此导致 .net 版本出现各种问题,visual studio 试图通过将绑定重定向添加到我的 web.config 中以将其指向 v4(并包括参考程序集)来对其进行排序,但未成功.

从 csproj 文件中删除所有 RequiredTargetFramework 元素已解决问题。

在我的例子中,我将 nuget 包 System.Collections 添加到我的 asp.net 4.6.1 项目中,但出于某种原因,它没有被引用到 csproj 文件中。我手动编辑了 csproj 文件并添加了引用。重新加载它,瞧,成功了!

我从 /Bin 文件夹中删除包

System.Collections and System.Collections.Concurrent

并重建项目

它的作品。

VS 15.8 更新后发生在我身上。将 "Copy Local" 设置为 false 解决了每个给我错误的程序集的问题。此外,我(手动)删除了 .csproj.

中重复的“<Private>”标签

如果您的绑定重定向引用的版本早于您的引用所依赖的版本,则经常会发生此问题,并且这可能会在更新包(例如,通过 NuGet)之后发生。为了解决一般问题,我添加了一系列步骤作为答案 。但是,对于这个特定问题,我特别建议执行第 5 步:

Remove all the assembly bindings from all app.config and Web.config files then build your solution. app.config bindings are not required anymore. Web.config bindings will be re-added in the next step, but removing them first ensures you don't have any outdated versions in your bindings.