开发者

Avoiding BadImageFormatException when there is unmanaged code

开发者 https://www.devze.com 2023-03-04 02:35 出处:网络
I have a web service that throws a BadImageFormatException any time I try to run the service from Visual Studio.This answer to another question suggested running peverify against DLLs to see if there

I have a web service that throws a BadImageFormatException any time I try to run the service from Visual Studio. This answer to another question suggested running peverify against DLLs to see if there are any problems. While my web service's DLL is fine, the web service depends on an ILMerged assembly, which does have errors:

[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)

I think these stem from my use of Marshal and IntPtr in MyILMergedAssembly, but everywhere I do that, I have the method declared as unsafe. How can I get rid of the errors peverify gives, and hopefully get rid of the BadImageFormatException that comes up when I try to run my web service?

Edit: it looks like maybe I can't use ILMerge to merge assemblies that have unsafe code. So I tried having my web service reference all the individual DLLs in MyILMergedAssembly, but I still get a BadImageFormatException. Now it gives me t开发者_如何转开发he exception on one of the individual assemblies. When I run peverify on that individual assembly, I get:

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

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

The ConnectionBase class to which it's referring is right there in Connection.dll, or at least it should be since the class is defined in the Connection namespace/project. Connection has 'Allow unsafe code' checked in its Build options. The constructor in ConnectionBase to which it might be referring is marked as internal.

Edit: I've tried checking 'Allow unsafe code' in the web service project that uses the DLLs with unsafe code. I also tried adding compilerOptions="/unsafe" to the Web.config file for the web service in the system.codedom > compilers > compiler node. I still get a page with the following when I run the web service, though:

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


Oh man, this is annoying. It wasn't unsafe code, it wasn't the 32/64-bit target platform settings on my projects in VS, it wasn't ILMerge. I had to set the Enable 32-Bit Applications setting to True on the application pool in IIS 7 (I'm in Windows 7 64-bit). After restarting the app pool, my web service loads fine. Thanks to this blog for sending me in the right direction.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号