模块应包含程序集清单



我在c#中有一个项目,它使用c++库和一些dll。当我运行它时,它运行得很好。

该库非常出色,同时包含c++库和c#包装器

当我从我的ASP。NET MVC 4项目我得到以下错误:

异常详细信息:系统。BadImageFormatException:无法加载文件或程序集"file:///D:\Projects\Development\ProGamers\GamingSite\bin\avcodec-52.dll或其依赖项之一。模块应包含程序集清单。

我尝试过的东西:

  1. 正在复制dll,但仍然无法工作(文件存在)。

  2. 在GAC中注册它,但我在那里遇到了同样的错误。

  3. 确保使用。NET Framework 4.0(而非客户端配置文件)

  4. 2个具有相同dll/exe名称的文件导致冲突-我没有它。

  5. 将解决方案的平台更改为AnyCPU/混合平台。

堆栈跟踪:

[BadImageFormatException: Could not load file or assembly 'file:///D:ProjectsDevelopmentProGamersGamingSitebinavcodec-52.dll' or one of its dependencies. The module was expected to contain an assembly manifest.]
   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.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark) +102
   System.Reflection.Assembly.LoadFrom(String assemblyFile) +34
   WebActivator.ActivationManager.Run() +190
[InvalidOperationException: The pre-application start initialization method Run on type WebActivator.ActivationManager threw an exception with the following error message: Could not load file or assembly 'file:///D:ProjectsDevelopmentProGamersGamingSitebinavcodec-52.dll' or one of its dependencies. The module was expected to contain an assembly manifest..]
   System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection`1 methods, Func`1 setHostingEnvironmentCultures) +550
   System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +132
   System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath) +90
   System.Web.Compilation.BuildManager.ExecutePreAppStart() +135
   System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +516
[HttpException (0x80004005): The pre-application start initialization method Run on type WebActivator.ActivationManager threw an exception with the following error message: Could not load file or assembly 'file:///D:ProjectsDevelopmentProGamersGamingSitebinavcodec-52.dll' or one of its dependencies. The module was expected to contain an assembly manifest..]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9850940
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
   System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) +456

我发现,我使用的是与目标不同的InstallUtil。NET框架。我正在建造一个。NET Framework 4.5,同时,如果我使用。NET Framework 2.0版本。为我的目标使用了正确的InstallUtil。NET Framework,解决了这个问题!

BadImageFormatException,根据我的经验,几乎总是与x86和x64编译的程序集有关。听起来您的C++程序集是为x86编译的,并且您正在x64进程上运行。这是正确的吗?

而不是使用AnyCPU/Mixed作为平台。试着手动将其设置为x86,然后看看它是否会在那之后运行。

首先尝试使用ILSpy等反编译器打开文件,您的dll可能已损坏。我在一个在线网站上遇到了这个错误,当我下载dll并试图打开它时,它已经损坏,可能是在通过ftp上传时发生了一些错误。

在我的案例中,我使用的是InstallUtil.exe,这导致了一个错误。要安装。Net Core服务窗口中使用sc命令的最佳方式。

此处的详细信息Exe安装引发错误模块应包含程序集清单。净核心

检查清单是否是有效的xml文件。我在构建结束时执行DOS复制命令时也遇到了同样的问题,结果发现由于某种原因,我无法理解"复制"在清单文件的末尾添加了一个奇怪的字符(->)。通过添加"/b"开关来强制二进制复制,问题得到了解决。

我在以下情况下得到了错误:

  1. A。NET EXE文件(#1)正在引用另一个。NET EXE文件(#2)
  2. 我试图用Themida混淆EXE#2
  3. 当运行EXE#1时,它崩溃,并出现错误"模块应包含程序集清单"

我只是避免了混淆,错误就消失了。不是一个真正的解决方案,但至少我知道是什么原因…

我发现了另一个奇怪的原因,我想可能是另一个开发者和我一样困惑。我确实在VS2010的开发者命令提示符中运行了install.bat来安装我的服务,但我的服务是在VS2012中生成的。它会出现这个错误,让我发疯,但我尝试了VS2012开发者命令提示符工具,一切都很好。我不知道为什么,但我的问题已经解决了。所以你可以测试它,如果有人知道原因,请与我们分享。谢谢。

正在删除。InstallState文件并重新安装服务为我修复了它。

在我的例子中,这是因为代码试图为不是C#程序集的DLL创建AssemblyCatalog。捕获BadImageFormatException并忽略任何这样的DLL是很容易的。不幸的是,Visual Studio(至少2017年)无法在BadImageFormatException s上中断。

最新更新