我在Visual Studio 2013中有一个Web项目,包括几个库项目。
问题是将一个引用(即System.Collection,System.Net)添加到Web项目中作为一个参考程序集添加'来自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可以解决问题,但我不确定需要更改什么才能正确修复此问题。
答案 0 :(得分:30)
我从 / Bin 文件夹
中删除了该包System.Collections 和 的 System.Collections.Concurrent 强>
并重建项目
它的作品。
答案 1 :(得分:6)
解决:强>
我的库项目引用了一些核心库(System.*
等),RequiredTargetFramework
选项设置为3.5。这只在csproj文件中很明显,例如:
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
因此.net版本引起各种各样的问题,visual studio试图通过在我的web.config中添加绑定重定向来将它们排序到v4(并包含引用程序集),但未成功。
从csproj文件中删除所有RequiredTargetFramework
个元素已解决了这个问题。
答案 2 :(得分:4)
每次看到BadImageFormatException
时,都会遇到二进制格式兼容性问题。可能是您的IIS池配置为运行32位池,您的程序集构建为x64,反之亦然。或者,您可能正在尝试在32位计算机上运行x64程序集。可能你有x64机器,anycpu构建的程序集,但是一些第三方程序集严格按照32位代码构建。
这是其中之一,或类似的
现在,“usr”在这里有好处。您确实有Cannot load a reference assembly for execution
但在BadImageFormatException
的上下文中。我想知道这是否在编译时发生。为此,请尝试将其添加到web.config
<compilation>
<assemblies>
<remove assembly="System.Collections" />
. . . .
或者,如果你有
<add assembly="System.Collections. . . ." />
首先尝试删除
现在,除非您提供probing
settings
答案 3 :(得分:0)
在我的情况下,我将nuget包System.Collections添加到我的asp.net 4.6.1项目中,并且由于某种原因,它没有被引用到csproj文件中。我手动编辑了csproj文件并添加了引用。重新加载它,瞧,工作了!
答案 4 :(得分:0)
在VS 15.8更新后,它发生在我身上。将“ Copy Local”设置为false可以解决导致该错误的每个程序集的问题。另外,我(手动)删除了.csproj中重复的“ import time
import numpy
from numba import jit
from ipyparallel import Client
client = Client() # create client and direct view to all engines available
dview = client[:]
dview.block = True
with dview.sync_imports():
import numpy
@jit
def TestingFnc(x):
a=x[0]
b=x[1]
c=x[2]
Result=0
for i in range(100000):
Result = Result + ((a+i)**2 + (b-i)**3) /(c+i)**3
return numpy.array([Result,1])
d=numpy.array([[0.,0.,0.],[0.,0.,1.],[0.,0.,2]])
Results = dview.map(TestingFnc, d)
”标签。
答案 5 :(得分:0)
如果您的绑定重定向所引用的版本早于引用所依赖的版本,则经常会发生此问题,并且这可能在更新软件包后(例如,通过NuGet)发生。为了解决一般问题,我添加了一系列步骤作为答案here。但是,对于此特定问题,我建议特别遵循以下步骤5:
从所有 app.config 和 Web.config 文件中删除所有程序集绑定,然后构建您的解决方案。 app.config 绑定不再需要。 Web.config 绑定将在下一步中重新添加,但是首先删除它们将确保您的绑定中没有任何过时的版本。