当我尝试访问IIS 7.0中的网站(使用Windows Server 2008上的VSTS 2010 + .Net 4.0开发)中的页面(default.aspx)时,我遇到以下错误消息。有什么想法有什么不对?什么意思 BadImageFormatException?
BTW:我正在运行Windows Server 2008 64位(不是R2)并且还没有激活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
答案 0 :(得分:16)
当可以找到程序集文件但是不正确的程序集或已损坏时,会引发BadImageFormatException
。例如,如果您将文件FTP到服务器并且传输被中断,则DLL文件可能只是部分传输,导致发生此错误。
在64位与32位上:当您使用P / Invoke或COM Interop时,切换到特定目标编译的some blogger reports可能有助于您的情况。这意味着:如果您使用32位dll进行交互,请确保为x86
编译,强制它在WoW32下运行,否则您将收到此异常。此修复程序为confirmed here和here。
或者,您可以通过运行命令将整个系统设置为默认的32位:
Ldr64.exe setwow
来自Framework64
目录。
常见的解决方案是重建文件,或者至少重新发布它。
答案 1 :(得分:7)
我在将32位DLL部署到运行IIS 7的64位服务器上时得到了这个。
要解决此问题,我必须在应用程序池的高级设置中将“启用32位应用程序”设置为True。
答案 2 :(得分:2)
您的网站是否使用DefaultAppPool? 如果是这样,请尝试将您的网站的应用程序池设置为ASP .Net v4.0, 或者,如果您使用的是自定义应用程序池,请验证它是否正在运行.net framework 4.0
答案 3 :(得分:0)
这是在VM中发生的,我需要在App-Pool中启用32位应用程序,并能够使用powershell做到这一点:
Import-Module WebAdministration
Set-ItemProperty IIS:\AppPools\DefaultAppPool\ -Name enable32BitAppOnWin64 -Value $true