gpt4 book ai didi

.net - 存在非托管代码时避免 BadImageFormatException

转载 作者:行者123 更新时间:2023-12-02 04:09:36 25 4
gpt4 key购买 nike

我有一个 Web 服务,每当我尝试从 Visual Studio 运行该服务时,它都会引发 BadImageFormatException。 This answer另一个问题建议对 DLL 运行 peverify 以查看是否有任何问题。虽然我的 web 服务的 DLL 很好,但 web 服务依赖于一个 ILMerged 程序集,它确实有错误:

[IL]: Error: [C:\blah\MyILMergedAssembly.dll : Encryption

Utils.SecureStringExtensions::SecureEquals][offset 0x00000055][found unmanaged pointer] [expected unmanaged pointer] Unexpected type on the stack.

[IL]: Error: [C:\blah\MyILMergedAssembly.dll : Encryption

Utils.SecureStringExtensions::SecureEquals][offset 0x0000005D][found unmanaged pointer][expected unmanaged pointer] Unexpected type on the stack.

[IL]: Error: [C:\blah\MyILMergedAssembly.dll : Encryption

Utils.SecureStringExtensions::SecureEquals][offset 0x0000007E] Unmanaged pointers are not a verifiable type.

3 Error(s) Verifying .\MyILMergedAssembly.dll

(188 Warnings)


我认为这些源于我对 Marshal 的使用。和 IntPtr在 MyILMergedAssembly 中,但在我这样做的任何地方,我都将方法声明为 unsafe .如何摆脱 peverify 给出的错误,并希望摆脱在尝试运行 Web 服务时出现的 BadImageFormatException?
编辑:看起来我可能无法使用 ILMerge 来合并具有不安全代码的程序集。所以我尝试让我的 Web 服务引用 MyILMergedAssembly 中的所有单个 DLL,但我仍然得到一个 BadImageFormatException。现在它给了我一个单独的程序集的异常(exception)。当我在那个单独的程序集上运行 peverify 时,我得到:

[IL]: Error: [C:\blah\Connection.dll : Connection

.ConnectionBase::.ctor] [HRESULT 0x80070002] - The system cannot find the file specified.

ConnectionBase它所引用的类就在 Connection.dll 中,或者至少应该是因为该类是在 Connection 命名空间/项目中定义的。 Connection 在其构建选项中选中了“允许不安全代码”。 ConnectionBase 中的构造函数它可能引用的标记为 internal .
编辑:我尝试在使用带有不安全代码的 DLL 的 Web 服务项目中检查“允许不安全代码”。我还尝试添加 compilerOptions="/unsafe"到 system.codedom > compilers > compiler 节点中 Web 服务的 Web.config 文件。但是,当我运行 Web 服务时,我仍然会得到一个包含以下内容的页面:

Could not load file or assembly 'Connection' or one of its dependencies. An attempt was made to load a program with an incorrect format.

最佳答案

哦,伙计,这很烦人。这不是不安全的代码,不是我在 VS 项目中的 32/64 位目标平台设置,也不是 ILMerge。我必须在 IIS 7 的应用程序池上将启用 32 位应用程序设置设置为 True(我在 Windows 7 64 位)。重新启动应用程序池后,我的 Web 服务加载正常。感谢 this blog把我送到正确的方向。

关于.net - 存在非托管代码时避免 BadImageFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5954491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com