【问题标题】:Could not load file or assembly... Windows Azure Website无法加载文件或程序集... Windows Azure 网站
【发布时间】:2014-10-22 11:51:09
【问题描述】:

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

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

我完全不知所措。

谢谢

史蒂夫

【问题讨论】:

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


    【解决方案1】:

    我讨厌这样做。在 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 网站平台(位于旧门户上的配置>平台下
    2. 检查您的所有项目是否设置为任何 CPU 或兼容的“平台(架构)”

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

    谢谢

    史蒂夫

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

    【讨论】:

    • 我在 Azure 网站上遇到了同样的错误,但错误出现在 Oracle.DataAccess 上。对我来说,在发布到 Azure 之前,我必须在 VS.NET 2019 中编辑一个设置。我在Tools > Options > Projects and Solutions > Web Projects 下设置了“为网站和项目使用 64 位版本的 IIS Express”。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2014-09-02
      • 2017-12-23
      • 2013-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-25
      • 2018-10-13
      • 2018-11-29
      相关资源
      最近更新 更多