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

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

本文介绍了.Net 4.6 网站未正确加载参考程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a web project in Visual Studio 2013, including several library projects.

问题是向 Web 项目添加引用(即 System.Collection、System.Net)是从 C:Program Files (x86)Reference AssembliesMicrosoft 添加为引用程序集",在 IIS 中加载时,它没有正确加载程序集的实现(来自 GAC).示例错误如下.

Problem is that adding a reference (ie. System.Collection, System.Net) to the web project is being added as a 'Reference Assembly' from C:Program Files (x86)Reference AssembliesMicrosoft, when loaded in IIS it is not correctly loading the implementation of the assembly (from GAC). Example error follows.

[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 可以解决此问题,但我不确定需要更改什么才能正确解决此问题.

Deleting the reference dlls from the bin folder fixes the issue, but I am not sure what needs changing to fix this properly.

推荐答案

解决方案:

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

My library projects were referencing some core libs (System.* etc) with the RequiredTargetFramework option set to 3.5. This was only evident in the csproj file, example:

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

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

Thus causing all sorts of issues with .net versions, visual studio was trying to sort it by adding binding redirects into my web.config to point these to v4 (and include reference assemblies), unsuccessfully.

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

Removing all RequiredTargetFramework elements from the csproj files has solved the problem.

这篇关于.Net 4.6 网站未正确加载参考程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 01:35