c# - 无法加载文件或程序集... Windows Azure 网站

标签 c# .net azure-web-app-service badimageformatexception

我知道周围有很多这样的帖子,我以前自己处理过这些帖子,除了这次没有任何问题。这是因为我无法从 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

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

我完全不知所措。

谢谢

史蒂夫

最佳答案

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

看来我错过了一条很有帮助的关键信息。

BadImageFormatException

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

然而,我的“Lib”项目仅为 x64 构建,而 Azure 网站在 32 位模式下运行,因此无法加载 64 位 dll。

两种选择:

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

我没有选择选项 2,因为我需要 64 位的“Lib”dll。

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

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

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

谢谢

史蒂夫

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

关于c# - 无法加载文件或程序集... Windows Azure 网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25551816/

相关文章:

.Net Core 3.x 与 .Net Framework 4.7 的兼容性

.net - 无法解决 Azure 应用服务中的 (502) 错误网关错误

azure - 身份验证后重定向到 http,而应该是 https

c# - 使用 .NET 绘图

.net - 带有 ASP.NET MVC 的 HttpModule 未被调用

c# - 我的 C# 程序被检测为病毒?

C# XMLDocument 到 DataTable?

c# - 如何在 C# 中针对流安全地创建 XPathNavigator?

c# - 实体 vs 聚合 vs 聚合根

c# - Lua 语言上下文/作用域实现