WCF service library : BadImageFormatException with

2019-07-21 10:33发布

问题:

My project Structure looks like this:

 myApp 
 -->WCFSerLib (Any CPU Deafult application) 
 -->ClassLib1 (Any CPU)
 -->ClassLib2 (x86)

When I try to execute my application Unfortuantely am getting this below error.

 System.BadImageFormatException was unhandled by user code

Is there anyway such that I can achieve the above?

Thanks

回答1:

If I guess right than you are trying to use an x86 dll on a x64 mashine. With AnyCPU the .net framework will use the currient architecture automatically.

So your application runs as a x64 Application which tries to load a x86 libary which failes. So you have two options:

  • Set the target architeture to x86
  • Add/create a x64 libary

If you have both verions (x86/x64) somewhere in you envireonment variable the operation system will choos the correct one automatically.



回答2:

I suppose that you are running your application on a x64 system.
Mixing the Platform Type in that way will cause the BFE when the WCFService Library or the ClassLib1 call methods in ClassLib2 because the first two run as 64bit processes while the latter is forced to run in x86 mode.

You should set the same platform type on every project.
Of course, the WCFService library supports only AnyCPU, so it make sense to use, for every project, the AnyCPU platform type.

If your ClassLib2 requires the use of a 32bit library (winscard.dll) and there is no 64bit version of that library, then, I think you should use a WCF Service Application instead of a Service Library. See this question



回答3:

Thank you guys,

problem was with winscard.dll it uses Handles which are Int32 .all I changed is replacing the Int32 with Int64.



标签: c# wcf .net-4.0