BadImageFormatException encountered with WcfSvcHost and IIS WCF host

白昼怎懂夜的黑 提交于 2019-12-01 20:49:37

a bit late but you can also change the app pool setting "Enable 32-bit Applications" to true in advanced settings.

Microsoft Product Support has resolved this question: It's by design. The unmanaged code is not loaded in the default AppDomain when using WcfSvcHost or the IIS WCF host.

A pure image will use a CLR version of the C run-time library. However, the CRT is not verifiable, so you cannot use the CRT when compiling with /clr:safe. For more information, see C Run-Time Libraries.

http://msdn.microsoft.com/en-us/library/k8d11d4s.aspx

I cannot provide an explanation for the error, only my initial suspicion that there is a permission difference between the context in which your code is run as a service and the context in which it is run when you place it in a console application. The E_HANDLE HRESULT is my clue here. Assuming that you run the console application as your logged-in user, you could try configuring the service to start as that user as well. If it works in that configuration, then you can attempt to narrow down what required resource is unavailable when it fails.

I can suggest a workaround. If there is an oddity of the DLL in question that prevents it from working in a hosted service, you can take the sacrificial process approach, so named because it is typically used to isolate a DLL that crashes often. Briefly, you create a proxy program whose only purpose is to load and call the DLL on behalf of your main process, using named pipes or some other IPC method to pass requests and results. If the DLL crashes, you start a new instance of the proxy program. In your case it would have the added benefit that only the wrapper program would need to be 32-bit.

Do you see anything special in the Event viewer?.
In Vista if there a manifest problem you will see traces of it in the Event Viewer, it will tell you to use SxsTrace.

Could you manually add a manifest to this DLL yourself, using the mt.exe file?

MSDN Article on using mt.exe

This may seem a bit dumb; but make sure your service is running in the correct application pool.

P.Brian.Mackey

Ran into this issue myself. I found a helpful post. As noted in other posts, Microsoft stated this is by design. Basically you need to:

  1. Locate your version of WcfSvcHost.exe. (for me and visual studio 2017: C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE)

  2. Launch developer command prompt

  3. Execute cmd: copy C:\SourcePath\WcfSvcHost.exe C:\DestinationPath\WcfSvcHost32.exe (Destination doesn't matter)

  4. cmd: corflags /32BIT+ /Force WcfSvcHost32.exe (may need to cd to DestinationPath

  5. In visual studio open the WCF project properties > Debug tab > Start External program: C:\DestinationPath\WcfSvcHost32.exe

  6. Also add your command line arguments:

    /service:MyWCFProjectName.dll /config:MyWCFProjectName.dll.config

    Note: You do not need to use ($ProjectDir) here

  7. Launch the app. You are now free to launch WcfServiceHost.exe seperately.

  8. Optionally go to solution > Set Startup Projects > Multiple Startup Projects > Choose the Wcf project and the client project.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!