1

OK, I am building a client server application using WCF with NetTcpBinding hosted and consumed by a WinForms application. I am using VS.2012 Ultimate in Windows 7 Ultimate x64.

I can start the winform that hosts the service and I can start the service. Now, on the client side I start the client application and immediately get a Microsoft WCF Service Host window telling me:

System.BadImageFormatException. Could not load file or assembly X.Services.dll ... an
attempt was made to load a program with an incorrect format.
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.Assembly.Load(AssemblyName assemblyRef)
at Microsoft.Tools.SvcHost.ServiceHostHelper.LoadServiceAssembly(String svcAssemblyPath)

Now, both the client and server WinForm projects have a reference to the X.Services.dll which defines the services (Interface & implementing class). So, why does the server doesn't complain about it?

Also, I have made sure that ALL the projects in this solution have BOTH their Debug & Release configurations set to the same .NET Framework (4.5) and the SAME target platform (x86 instead of Any CPU).

Additionally, the only external assembly reference (a utility library DLL) is ALSO built with the same VS version, for the same framework (4.5) AND the same platform (x86).

So where does this bad image format comes from? The solution has its own copy of the external library (to make sure it has the right platform & framework configuration) and the project files have been modified so that it takes the corresponding Release/Debug version of the external assembly.

Following a hint from the exception I used regedit to add a key in HKLM to enable assembly load/binding logging and got this extra information:

=== Pre-bind state information ===
LOG: User = DOMAIN\Username
LOG: Where-ref bind. Location =    D:\Sources\My.Net\Code\T\X.Services\bin\Debug\X.Services.dll
LOG: Appbase = file:///D:\Sources\My.Net\Code\T\X.Services\bin\Debug\X.Services/bin/Debug
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be      probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: D:\Sources\My.Net\Code\T\X.Services\bin\Debug\X.Services.dll.config
LOG: Using host configuration file: 
LOG: Using machine configuration file from  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Attempting download of new URL file:///D:\Sources\My.Net\Code\T\X.Services\bin\Debug\X.Services.dll.
ERR: Failed to complete setup of assembly (hr = 0x8007000b). Probing terminated.

which to me doesn't tell me anything of use...

Lord of Scripts
  • 3,579
  • 5
  • 41
  • 62

2 Answers2

0

May be the problem in:

LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.

It seems that application starts under .NET 4.0 x64. Try to analyze your assembly (for example, by corflags.exe from .NET Framework SDK) to determine required platform and .NET version.

Gendolph
  • 463
  • 7
  • 12
0

I don't know if this is still active, but it's the same question as in this thread. The solution is to run every process in x86 like here.

It's the easiest to create a second WcfSvcHost as a x86 variant like this:

start an cmd environment with admin privileges:

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

Then you can host your project with the command:

WcfSvcHost32.exe /service:%enteryourservicenamehere%.dll /config:%yourconfigfilehere.dll.config%

that's what worked for me.

Community
  • 1
  • 1
Uke
  • 169
  • 3
  • 21