无法加载引用程序集以供执行

Kru*_*nal 22 asp.net visual-studio acumatica visual-studio-2017

突然我的网站没有加载并给我以下错误.我在Windows 10 Home上运行带有.Net Framework 4.7.1的VS2017.

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

[BadImageFormatException: Could not load file or assembly 'System.IO.Compression.ZipFile' 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.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +225
   System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection) +110
   System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +22
   System.Reflection.Assembly.Load(String assemblyString) +34
   System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +48

[ConfigurationErrorsException: Could not load file or assembly 'System.IO.Compression.ZipFile' 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) +729
   System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +247
   System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +157
   System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +226
   System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies() +73
   System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +321
   System.Web.Compilation.BuildManager.ExecutePreAppStart() +170
   System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +734

[HttpException (0x80004005): Could not load file or assembly 'System.IO.Compression.ZipFile' 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) +525
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +118
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +708
Run Code Online (Sandbox Code Playgroud)

有什么建议?

小智 35

对我有用的是删除我的web应用程序下的bin和obj目录然后重建.

  • 这可行,但在我重新启动应用程序几次后问题又回来了。 (4认同)
  • 我很沮丧 我在这浪费了一个小时,做了大量的Clean,但是,你是对的...删除obj和bin文件夹为我解决了这个问题.非常感谢.(叹...!) (2认同)
  • 我也是。我做了清理,重新启动 NuGet 恢复和更新,但没有任何效果。我什至删除了我的 bin 文件夹。显然,同时删除 bin 和 obj 是诀窍。 (2认同)
  • 是的,这成功了!您确实需要删除目录(只是清空它是行不通的) (2认同)

ofl*_*ero 6

.NET Framework 4.7.1在这方面存在一个已知问题。据说是在4.7.2中排序的,但是与此同时您可以做什么呢?

该问题与序列化程序集有关,您可以选择将其设置为生成或不生成的一部分(单击项目->属性->生成选项卡->请参阅底部的“生成序列化程序集”。)

对我有用的-我在这里部分站在别人的肩膀上-是确保将此设置设置为“自动”。做一个完整的“干净的解决方案”,此外,如果偏执狂的话,如果在您的解决方案的根文件夹中运行此PowerShell代码段,也很方便:

Get-ChildItem .\ -include bin,obj -Recurse | foreach ($_) { remove-item $_.fullname -Force -Recurse }

接下来,将以下目标添加到您的csproj中(就在封闭<Project>标记内:

<Target Name="RemoveDesignTimeFacadesBeforeSGen" BeforeTargets="GenerateSerializationAssemblies">
    <ItemGroup>
      <ReferencePath Remove="@(_DesignTimeFacadeAssemblies_Names->'%(OriginalIdentity)')" />
    </ItemGroup>
    <Message Importance="normal" Text="Removing DesignTimeFacades from ReferencePath before running SGen." />
  </Target>
  <Target Name="ReAddDesignTimeFacadesBeforeSGen" AfterTargets="GenerateSerializationAssemblies">
    <ItemGroup>
      <ReferencePath Include="@(_DesignTimeFacadeAssemblies_Names->'%(OriginalIdentity)')" />
    </ItemGroup>
    <Message Importance="normal" Text="Adding back DesignTimeFacades from ReferencePath now that SGen has run." />
  </Target>
Run Code Online (Sandbox Code Playgroud)

如果仍然不走运,请将“ Generate serialization Assemblys”显式设置为“ On”以强制生成,然后重新生成并运行。


小智 6

我也多次遇到这个问题,这个错误可能是由于 bin 文件夹中的 dll 的引用或加载问题引起的。只需删除 .bin 文件夹并重建您的应用程序即可工作。

快乐编码。


Mur*_*nan 5

今天我安装了 Acumatica 2018 R1,遇到了这个问题。从 bin 文件夹中删除 System.IO.Compression.ZipFile 解决了这个问题。