【问题标题】:.Net 4.6 website not loading Reference Assemblies correctly.Net 4.6 网站未正确加载参考程序集
【发布时间】:2015-09-24 15:53:36
【问题描述】:

我在 Visual Studio 2013 中有一个 Web 项目,包括几个库项目。

问题是向 Web 项目添加引用(即 System.Collection、System.Net)是作为来自 C:\Program Files (x86)\Reference Assemblies\Microsoft 的“引用程序集”添加的,当在 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 可以解决此问题,但我不确定需要更改哪些内容才能正确解决此问题。

【问题讨论】:

  • 答案已更新 [下方]
  • 我实际上可能确切地知道您的问题是什么:您必须在您对System.Collections 的引用上检查过Copy Local = true。现在你在 bin 和其他地方都有它。您的网站尝试在 bin 中编译一个。所以,是的,下面的答案可能只是用<remove assembly> 修复它。但真正的解决方法是设置Copy Local = false。您通常不想在本地复制任何 System.* 程序集。
  • @T.S.好主意,我检查了所有项目,没有一个有问题的引用被设置为复制本地
  • 那么问题仍然存在。 System.Collections 如何进入垃圾箱?尝试分别构建每个项目并检查输出。一定有某个项目可以复制它。
  • @T.S.是的,仍然是一个问题。我认为它的目标是.net 4.6。 iis 中的默认程序集是 4.0,因此这些将提高版本(此时的理论)。

标签: c# asp.net .net iis .net-assembly


【解决方案1】:

我从 /Bin 文件夹中删除包

System.CollectionsSystem.Collections.Concurrent

并重建项目

它的作品。

【讨论】:

    【解决方案2】:

    分辨率:

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

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

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

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

    【讨论】:

    • 我永远不会认为有人会为系统文件这样做:复制本地、所需的框架或所需的特定版本。不幸的是,我停止复制本地
    • 所以,如果没有真正在您的计算机上,您的问题实际上并不容易解决。可能性是无穷无尽的。我们只知道这么多——你加载了错误的程序集。 &lt;remove&gt; 停止了错误的加载,但没有完全解决问题。这表明提供尽可能多的信息的重要性
    • 我正在使用 .NET Core,必须从 csproj 文件中的 HintPath 中删除违规引用。这个答案为我指明了正确的方向。谢谢!
    【解决方案3】:

    任何时候你看到BadImageFormatException,你都会遇到二进制格式兼容性的问题。可能是您的 IIS 池配置为运行 32 位池,并且您的程序集构建为 x64,反之亦然。或者,您可能尝试在 32 位机器上运行 x64 程序集。可能你有 x64 机器,anycpu 构建的程序集,但某些第 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 是首选参考位置是正常的

    【讨论】:

    • 还要考虑应用程序池可能正在为多个站点提供服务,并且它可能是加载了不匹配位数的其他站点之一。
    • 错误消息是Cannot load a reference assembly for execution,它指向位数不匹配。
    • @usr Thaks 指出了这一点。我更新了答案。可能是这样,或者可能是在那之前的东西。并查看我对问题的评论
    • @T.S.谢谢!删除给出错误的程序集修复了问题
    【解决方案4】:

    在 VS 15.8 更新后发生在我身上。将“复制本地”设置为 false 解决了每个给我错误的程序集的问题。此外,我(手动)删除了 .csproj 中重复的“&lt;Private&gt;”标签。

    【讨论】:

      【解决方案5】:

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

      从所有 app.configWeb.config 文件中删除所有程序集绑定,然后构建您的解决方案。 app.config 绑定不再需要。 Web.config 绑定将在下一步中重新添加,但首先删除它们可确保您的绑定中没有任何过时的版本。

      【讨论】:

        【解决方案6】:

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

        【讨论】:

          猜你喜欢
          • 2018-02-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多