Debugging a WCF Service Using a 32 Bit Dll
Recently I was involved in a problem where we had a WCF service referencing a 32 bit dll. The service was set to to “Start WCF Service Host when debugging another project in the same solution”. Unfortunately that ended up with an exception.
Could not load file or assembly ‘My32BitLib, Version=184.108.40.206, Culture=neutral, PublicKeyToken=null’ or one of its dependencies. An attempt was made to load a program with an incorrect format.
When a CLR process starts, the exe file determines if it will be loaded as a 32 or 64-bit process. In this case the
WcfSvcHost.exe is started as a 64-bit process, loads the service dll (compiled as “Any CPU”) and then fails when trying to load the My32BitLib assembly which is compiled as “x86”.
The solution is to create a special 32bit version of
WcfSvcHost and setup the debugging environment to use that instead of the standard
Changing the Service Library itself to X86
A first attempt to solve the problem was to change the service library itself to x86. I thought that it would be natural for Visual Studio to check the platform target of the service class library and use a corresponding
Unfortunately I was wrong. WcfSvcHost pops up an error message.
System.BadImageFormatException: Could not load file or assembly ‘file:///C:\Users\andabe\Documents\SVN\BlogCode\trunk\TestService\bin\Debug\TestService.dll’ or one of its dependencies. An attempt was made to load a program with an incorrect format.
StackOverflow to the Rescue
When having this kind of problems google is often the one and only thing that helps. I found a StackOverflow Question which hinted with a link about using the
Setting up 32-bit Debugging
To set up a robust, reusable 32-bit debugging environment for WCF services I want to create a separate version of
As a command line geek I launch a Visual Studio Command Prompt in administrator mode.
c:\ >cd "c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE" c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE >copy WcfSvcHost.exe WcfSvcHost32.exe 1 file(s) copied. c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE >corflags /32BIT+ /Force WcfSvcHost32.exe Microsoft (R) .NET Framework CorFlags Conversion Tool. Version 4.0.30319.1 Copyright (c) Microsoft Corporation. All rights reserved. corflags : warning CF011 : The specified file is strong name signed. Using /Force will invalidate the signature of this image and will require the assembly to be resigned. c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE >
WcfSvcHost32 created the next step is to make Visual Studio use it. The option to “Start WCF Service Host when debugging another project in the same solution” won’t work any more, so first uncheck that option in the service project’s properties on the “WCF Options” tab. Then switch over to the “Debug” tab and manually configure the class library to be debugged with
To debug the WCF service together with the client program, the option to use multiple startup projects can be used to launch both of them together. Bring up the context menu of the solution itself in solution explorer and choose “Set StartUp Projects…”.
Unit Tests Against the Service
When writing the code for this blog, I use a test project to be able to easily run different snippets of test code. To run my test case that calls the WCF service I first have to launch the service project. From the context menu of the project, choose debug. That should bring up a small popup from the task bar. Once the Wcf service is hosted, the test cases can be run as usual.
Paolo Antonucci on 2013-09-24
Very cool, thank you, I will be grateful to you for years! :-) You saved me… possibly some hours of work!
carlos on 2013-09-27
helped me a lot thanks
sandy on 2014-02-12
After doing all steps while running service I m getting following pop up message
Please help me how do I compile wcf service library in 32 bit mode . I m referencing 32 bit dll inside wcf service which gives me BadImageFormatException error . After doing in WCF service application also it wont work
Tamer on 2014-06-03
Hi,i did what you said and i got error
and solved it by doing with WcfTestClient.exe as we did with WcfSvcHost and add it in the Command like this
but now i can’t add the service to the Host ,and it gives me the same error as before.
all projects in the solution are 32 bit
John on 2014-09-19
This was of great value. Thank you
Markus on 2015-03-12
Live saver! Thank you very much!
Mer on 2015-10-23
Great!! Thanks a lot!!
Mr.D on 2017-11-15
Tq Anders.. Its very helpful!
Leonardo Orazi on 2018-05-01
Many Many thanks … you saved me a lot of time !!!