【问题标题】:Reference assemblies should not be loaded for execution不应加载引用程序集以执行
【发布时间】:2017-03-05 13:33:27
【问题描述】:

当我尝试在 Visual Studio 中调试它时,我的网站突然抛出此错误消息。如果我部署网站,它可以正常工作。

解决此错误的任何建议。

[ConfigurationErrorsException:无法加载文件或程序集“Microsoft.Win32.Primitives”或其依赖项之一。不应加载引用程序集以供执行。它们只能在 Reflection-only loader 上下文中加载。 (HRESULT 异常:0x80131058)] 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,异常 appDomainCreationException)+531 [HttpException (0x80004005): 无法加载文件或程序集“Microsoft.Win32.Primitives”或其依赖项之一。不应加载引用程序集以供执行。它们只能在 Reflection-only loader 上下文中加载。 (HRESULT 异常:0x80131058)] System.Web.HttpRuntime.FirstRequestInit(HttpContext 上下文) +9946132 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext 上下文)+90 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest WR,HttpContext 上下文)+261

【问题讨论】:

    标签: asp.net-mvc visual-studio


    【解决方案1】:

    当我从“[build folder]\ref”文件夹中附加我的 DLL 时,也会发生同样的错误。

    应该从 [build 文件夹] 中引用它。 (Net5)

    【讨论】:

      【解决方案2】:

      我注意到我的dll只有1.0,错误框说4.0

      我回到 NuGet 包管理器并放入 System.Management.dll 4.0 —

      不走运,然后我只好回去单独放入 System.Management.dll(我记得),它提出了 5.0.不完全确定我是如何促使它这样做的,但它似乎很愿意。

      已安装,工作正常!

      【讨论】:

        【解决方案3】:

        删除\bin目录,重建你的项目。

        【讨论】:

        • 这对我有用。我切换了目标框架,所以 Clean 忽略了 \bin 文件夹中的文件,这些文件与我的项目混淆了。
        【解决方案4】:

        我已经从本地删除了现有的代码,并再次克隆了代码。后记它开始为我工作。

        【讨论】:

          【解决方案5】:

          如果您正在使用 .NET Core,并且有这个:

          using System.Data.SqlClient;
          

          然后,将其切换到这个是一个潜在的解决方案:

          using Microsoft.Data.SqlClient;
          

          【讨论】:

          • 我遇到了System.Data.SqlClient 的这个问题,并且能够通过从 NuGet 安装包来解决它。 reference assembly 一定是在某处被引用而不是“真实”的。
          • 谢谢,吉姆。您的解决方案为我的 .NET Core 3.1 应用程序指明了正确的方向。在我的例子中,我也将 NLog 与数据库目标一起使用。更正引用后,我不得不采取额外的步骤来设置 dbProvider="Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient",现在我可以开始了!参考:github.com/NLog/NLog/wiki/Database-target#dbprovider-examples
          【解决方案6】:

          我遇到了这个问题,但是在错误消息中指定了System.Runtime

          [BadImageFormatException: Could not load file or assembly 'System.Runtime' 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)]
          

          我发现彻底清理解决方案并重建为我解决了这个问题。仅仅做一个 Visual Studio Clean 是不够的。我实际上也不得不删除我的\bin 文件夹。

          【讨论】:

          • 对我来说这是在部署中。我没有清理现有的 dll,并且也得到了 BadImageFormat。清除所有内容并重新部署为我修复了它。
          【解决方案7】:

          尝试从您的 web.config 中删除该部分

          <dependentAssembly>
              <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
              <bindingRedirect oldVersion="0.0.0.0-4.0.10.0" newVersion="4.0.10.0" />
            </dependentAssembly>
          

          【讨论】:

          • @AllmanTool 非常感谢,你拯救了我的一天。你能解释一下为什么我们需要删除这个部分吗?
          • 你好Chau Than,其实我不知道:) 我认为它与依赖重定向有关。我曾经遇到过这样的问题,并在互联网上找到了这个答案
          • 这对我也有用,因为我的 API 突然不再调试(我升级了内部 NuGet 包)。
          • 我不得不删除很多东西......,可能是 10 到 15 个......,但它最终会奏效。 ?
          猜你喜欢
          • 1970-01-01
          • 2017-12-14
          • 2021-05-02
          • 2020-08-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-28
          • 2023-04-05
          相关资源
          最近更新 更多