【问题标题】:BadImageFormatException in a service服务中的 BadImageFormatException
【发布时间】:2012-06-23 20:32:08
【问题描述】:

我有一个应用程序是一项长期运行的服务,我最近重写了它以使用插件和 appdomains 来解决内存问题,结果证明这不是我的问题,而是 Microsoft 的 DirectoryServices 命名空间中的泄漏,没有正确处理东西。在我重写之后,我不能再将应用程序作为服务运行,尽管它在控制台模式下运行良好。尝试启动应用程序时,我在 Windows 事件日志中收到以下异常:

Application: ADImport.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.BadImageFormatException
Stack:
   at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
   at System.Reflection.Assembly.LoadFrom(String assemblyFile)
   at ADImportPlugIn.LoadAssemblyAttributesProxy.LoadAssemblyAttributes(String assFile)
   at ADImportPlugIn.LoadAssemblyAttributesProxy.LoadAssemblyAttributes(System.String)
   at ADImportPlugIn.PlugInLoader.GetAssemblyNames()
   at ADImportPlugIn.PlugInLoader.LoadAllPlugIns()
   at ADImport.Program.WorkerMethod()
   at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.ThreadHelper.ThreadStart()

我在这里搜索并看到了许多关于应用程序位数以及应用程序与相关 DLL/程序集之间可能不匹配的线程。我检查了所有内容,尝试针对特定平台等,似乎没有任何效果。我的开发平台是 64 位 Windows 7,目标是 Windows 2008 64 位。我的应用程序设置为 x86,我的组件是 anycpu,尽管我也尝试将它们设置为 x86、x64 等。我在我的一个 DLL 中使用 log4net,但不认为这是问题。

任何帮助将不胜感激!

【问题讨论】:

  • 尝试将您的应用程序设置为 AnyCpu...
  • "LoadAllPlugIns" 是一个危险的名称。您是完全过滤还是只是寻找和收集您找到的任何 DLL?捕获异常,添加日志记录。
  • @hans 我在过滤,但我在错误的目录中过滤!感谢您的提示,它让我找到了解决方案……它在我的 pluginloader 程序集中。

标签: c# exception appdomain badimageformatexception


【解决方案1】:

如上所述,我正在过滤,但做错了。我有一个正在使用的程序集

string[] fileNames = Directory.GetFiles(Environment.CurrentDirectory, "*.dll");

我需要将其更改为:

string[] fileNames = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll");

忘记了,因为这是一个 Windows 服务,当前目录不是应用程序的安装位置,而是 Windows\System32。呵呵。

【讨论】:

    猜你喜欢
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-06
    • 1970-01-01
    • 2011-08-20
    • 1970-01-01
    相关资源
    最近更新 更多