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

Ste*_*tes 10 .net c# badimageformatexception azure-web-sites

我知道这些帖子很多,我之前已经处理过这些问题,除了这次之外没有任何问题.这是因为我无法从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
Run Code Online (Sandbox Code Playgroud)

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

我完全失去了.

谢谢

史蒂夫

Ste*_*tes 16

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

所以我似乎错过了一些关键信息,这些信息会有很大帮助.

BadImageFormatException

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

但是我的"Lib"项目仅为x64构建,Azure网站以32位模式运行,因此无法加载64位dll.

两种选择:

  1. 将"Lib"dll编译为AnyCPU或32位然后重新发布
  2. 将天蓝色网站切换为64位.

我没有选项2,因为"Lib"dll的作用我需要64位.

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

  1. Azure网站平台(位于旧门户上的Configure> Platform下)
  2. 检查所有项目是否为任何CPU或兼容的"平台(架构)"设置

我希望这有助于其他人.

谢谢

史蒂夫

编辑:如果其他人有一些更有用的信息,为将来可能有这个问题的人添加,请做.