C# .Net程序集加载问题期间出现BadImageFormatException

C# .Net程序集加载问题期间出现BadImageFormatException,c#,asp.net,.net,visual-studio-2010,iis,C#,Asp.net,.net,Visual Studio 2010,Iis,当我尝试访问IIS 7.0(在Windows Server 2008上使用VSTS 2010+.Net 4.0开发)网站中的页面(default.aspx)时,遇到以下错误消息。你知道怎么了吗?什么意思 BadImageFormatException 顺便说一句:我正在运行64位(不是R2)的Windows Server 2008,但尚未激活Windows Server 2008,这可能是根本原因吗 [BadImageFormatException: Could not load file or

当我尝试访问IIS 7.0(在Windows Server 2008上使用VSTS 2010+.Net 4.0开发)网站中的页面(default.aspx)时,遇到以下错误消息。你知道怎么了吗?什么意思 BadImageFormatException

顺便说一句:我正在运行64位(不是R2)的Windows Server 2008,但尚未激活Windows Server 2008,这可能是根本原因吗

[BadImageFormatException: Could not load file or assembly 'test.foo' or one of its dependencies. Try to load bad formatted program. ] System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0 System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection, Boolean suppressSecurityChecks) +567 System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +192 System.Reflection.Assembly.Load(String assemblyString) +35 System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +118 [ConfigurationErrorsException: Could not load file or assembly 'test.foo' or one of its dependencies. Try to load bad formatted program. ] System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +11392147 System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +484 System.Web.Configuration.AssemblyInfo.get_AssemblyInternal() +127 System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +334 System.Web.Compilation.BuildManager.CallPreStartInitMethods() +280 System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +1087 [HttpException (0x80004005): Could not load file or assembly 'test.foo' or one of its dependencies. Try to load bad formatted program. ] System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +11524352 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +141 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +4782309 [BadImageFormatException:无法加载文件或程序集“test.foo”或其依赖项之一。请尝试加载格式错误的程序。] System.Reflection.RuntimeAssembly.\u nLoad(AssemblyName文件名、字符串代码基、证据assemblySecurity、RuntimeAssembly位置提示、StackScrawMark和stackMark、Boolean throwOnFileNotFound、Boolean ForInspection、Boolean suppressSecurityChecks)+0 System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef,证据assemblySecurity,StackScrawMark&stackMark,用于内省的布尔值,布尔值suppressSecurityChecks)+567 System.Reflection.RuntimeAssembly.InternalLoad(字符串assemblyString、证据assemblySecurity、StackScrawMark和stackMark、布尔值ForInterspection)+192 System.Reflection.Assembly.Load(字符串assemblyString)+35 System.Web.Configuration.CompilationSection.LoadAssemblyHelper(字符串assemblyName,Boolean starDirective)+118 [ConfigurationErrorsException:无法加载文件或程序集“test.foo”或其依赖项之一。请尝试加载格式错误的程序。] System.Web.Configuration.CompliationSection.LoadAssemblyHelper(字符串assemblyName,布尔starDirective)+11392147 System.Web.Configuration.CompliationSection.LoadAllassembliesFromAppDomainBindDirectory()+484 System.Web.Configuration.AssemblyInfo.get_AssemblyInternal()+127 System.Web.Compilation.BuildManager.GetReferenceAssembly(CompilationSection compConfig)+334 System.Web.Compilation.BuildManager.CallPreStartInitMethods()+280 System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager、IApplicationHost appHost appHost、IConfigMapPathFactory configMapPathFactory、HostingEnvironmentParameters hostingParameters、PolicyLevel PolicyLevel、Exception appDomainCreationException)+1087 [HttpException(0x80004005):无法加载文件或程序集“test.foo”或其依赖项之一。请尝试加载格式错误的程序。] System.Web.HttpRuntime.FirstRequestInit(HttpContext上下文)+11524352 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext上下文)+141 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequestWR,HttpContext上下文)+4782309
您的网站是否使用DefaultAppPool? 如果是,请尝试将网站的应用程序池设置为ASP.Net v4.0,
或者,如果您使用的是自定义应用程序池,请验证它是否正在运行.net framework 4.0

当可以找到程序集文件但不是正确的程序集或已损坏时,会引发
BadImageFormatException
。例如,如果您将文件FTP到服务器,并且传输被中断,则DLL文件可能仅部分传输,从而导致发生此错误

64位vs 32位:当您使用p/Invoke或COM互操作时,切换到特定的目标编译可能会有助于您的情况。这意味着:如果您与一个32位dll接口,请确保您为
x86
编译,强制它在WoW32下运行,否则您将收到此异常。此修复是和

或者,您可以通过运行以下命令将整个系统设置为默认32位:

Ldr64.exe setwow Ldr64.exe setwow 从
Framework64
目录


一个常见的解决方案是重建文件,或者至少重新发布它。

我刚在运行IIS 7的64位服务器上部署32位DLL时得到了这个解决方案


要修复它,我必须在我的应用程序池的高级设置中将“启用32位应用程序”设置为True。

这发生在我的虚拟机上,我需要在应用程序池中启用32位应用程序,并且可以使用powershell实现这一点:

Import-Module WebAdministration
Set-ItemProperty IIS:\AppPools\DefaultAppPool\ -Name enable32BitAppOnWin64 -Value $true

我认为您的程序集名称缺少“.dll”。请提供代码段。64位/32位不匹配?您是否为特定平台编译了程序集?@George2:它确实不在您的代码中,程序集加载器会引发此异常(您可以拥有自己的程序集加载器,但这里似乎不是这样)。关于32/64不匹配:在64位上运行时指定32位(为什么要看到我的答案)。@George2-通常不应该指定平台类型。您应该使用“任意CPU”,并让主机\框架在运行时选择平台。当前选定的平台类型显示在VS菜单栏的框中。如果它没有显示,您可以从工具->自定义->命令->添加命令->构建->解决方案平台添加它SPS:升级到R2可能是一件好事。微软已经修复了大量问题,其中不少与.NET有关。因此,如果提到的修复没有帮助或不适用,请确保将所有内容升级到其最新版本(!)。我正在使用一个新的应用程序池。我已经将其设置为使用.NET4.0框架,并使用集成模式。有什么想法吗?这个问题可能是由于我的Windows Server 2008未激活(我还没有输入许可证号)造成的吗?您正在使用哪个平台构建解决方案?有CPU吗?x86?我不认为这是一个激活问题。你在使用第三方吗?我没有使用第三方。如果我指的是我自己开发的另一个DLL,它是第三方的吗?这个DLL是用哪个平台构建的?你是说test.foo.DLL本身损坏了还是其他一些系统DLL损坏了?@geoge2:我是说test.foo.DLL损坏了,但是请看我关于32位对64位的新说明,这似乎有点违反直觉。谢谢你的更新@Abel,你是说我应该将解决方案重新构建到64位平台(因为我的Windows Server 2008是64位的)?@George2:这取决于具体情况。如果不对32位COM或本机DLL使用COM互操作或P/Invoke,则可以坚持使用任何,问题是