11

我知道周围有很多这样的帖子,我以前自己处理过这些帖子,除了这次没有问题。这是因为我无法从 Windows Azure 中获取所需的调试信息,并希望有人能够帮助我。

在我的本地环境、调试和发布中,这一切都很好。

我收到此错误:无法加载文件或程序集“Lib”或其依赖项之一。试图加载格式不正确的程序。

Lib 确实存在于目录中,它仅依赖于 System.Drawing 和 mscorlib。

通常我会AppDomain.Current.AssemblyResolve在抛出异常时附加或检查异常。我无法使用 Azure 执行此操作,因为异常发生在预加载过程中。

    [BadImageFormatException: Could not load file or assembly 'Lib' or one of its dependencies. An attempt was made to load a program with an incorrect format.]
   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 'Lib' or one of its dependencies. An attempt was made to load a program with an incorrect format.]
   System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +736
   System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +217
   System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +130
   System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +170
   System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies() +91
   System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +284
   System.Web.Compilation.BuildManager.ExecutePreAppStart() +153
   System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +521

[HttpException (0x80004005): Could not load file or assembly 'Lib' or one of its dependencies. An attempt was made to load a program with an incorrect format.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9930568
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254

有没有办法覆盖/创建自定义程序集加载器来拦截这个问题?

我完全不知所措。

谢谢

史蒂夫

4

2 回答 2

17

当我这样做时,我讨厌它。在 SO 上发布一个问题,因为我整天都在尝试,然后我在 10 分钟后修复它。

因此,我似乎错过了一条很有帮助的关键信息。

BadImageFormatException

当平台(架构)兼容性(x86,x64)存在问题时,会抛出这个(据我所知)。我的所有项目都是为“任何 CPU”编译的(可以在项目属性 > 构建 > 平台目标,VS2013 下找到)。

但是,我的“Lib”项目仅针对 x64 构建,并且 Azure 网站在 32 位模式下运行,因此无法加载 64 位 dll。

两种选择:

  1. 将“Lib”dll 编译为 AnyCPU 或 32 位,然后重新发布
  2. 将 azure 网站切换到 64 位。

我没有使用选项 2,因为“Lib” dll 我需要它作为 64 位。

因此,如果其他人有类似的情况,以供将来参考,请检查以下内容:

  1. Azure 网站平台(在旧门户上的配置>平台下找到
  2. 检查所有项目的设置是否适用于任何 CPU 或兼容的“平台(架构)”

我希望这对其他人有帮助。

谢谢

史蒂夫

编辑:如果其他人有一些更有用的信息要为将来可能遇到此问题的人添加,请执行。

于 2014-08-28T15:24:09.760 回答
0

我知道这个问题是关于“Lib”的,这不是我的情况。但是我有BadImageFormatException一个Azure App Service,这是我发现的最接近的问题。原来我使用 appType webAppLinux而不是webAppWindows(管道部署)部署了应用程序,并且启用了基于代理的监控 Application Insights,并且它在 Linux 中不受支持。所以对于那种问题,关闭基于代理的监控解决了我的问题。顺便关掉后需要重启应用。

于 2020-06-20T15:26:35.133 回答