我有一个使用Visual Studio 2010编写的基于C#语言、针对.NET Framework 4.0的Windows服务。在调试版本下可以正常运行,但是在发布版本下遇到了System.BadImageFormatException异常(以下为详细信息)。我已经在互联网上搜索了很多解决方案,但至今没有找到合适的。
这个问题在Windows 7 64位系统和Windows XP SP3 32位系统中都存在。
以下是我尝试过的方法:
- 验证生成设置,例如平台目标是否相同(x86)。
- 使用带有/verbose选项的peverify确保程序集二进制文件有效。
- 使用fuslogvw查找任何加载问题。
- 使用CheckAsm查找缺失的文件或程序集。
所有这些检查都没有改变任何东西。我在下面附上了异常信息的全部文本,其中一些名称已更改以保护我的公司机密。
System.BadImageFormatException was unhandled Message=Could not load file or assembly 'XxxDevices, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format. Source=XxxDevicesService FileName=XxxDevices, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null FusionLog=Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll Running under executable c:\Dev\TeamE\bin\Release\XxxDevicesService.vshost.exe --- A detailed error log follows.日志:此绑定在默认加载上下文中开始。 日志:使用应用程序配置文件:c:\ TeamE \ bin \ Release \ XxxDevicesService.vshost.exe.Config 日志:使用主机配置文件: 日志:使用计算机配置文件C:\ Windows \ Microsoft.NET \ Framework64 \ v4.0.30319 \ config \ machine.config。 日志:此时未将策略应用于引用(私有、自定义、部分或基于位置的程序集绑定)。 日志:尝试下载新的URL文件:///c:/TeamE/bin/Release/XxxDevices.DLL。 错误:无法完成程序集设置(hr = 0x8007000b)。探测终止。
=== Pre-bind state information === LOG: User = XXX LOG: DisplayName = XxxDevices, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null (Fully-specified) LOG: Appbase = file:///c:/Dev/TeamE/bin/Release/ LOG: Initial PrivatePath = NULL Calling assembly : XxxDevicesService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null. ===
StackTrace: 在XxxDevicesService.Program.Main(String[] args) 在System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 在System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 在System.Threading.ThreadHelper.ThreadStart() InnerException:
XxxDevicesService
是什么类型的程序集?它是否为特定平台编译(例如32位)?如果是这样,那么你必须将你的平台编译成32位。 - Reddog