System.BadImageFormatException - can't solve it


  • Hello

    I'm using Visual Studio 2010, running in Windows 7 X64
    I was trying to create a simple WCF implementation of duplex communication.
    Sadly enough I got stuck very quickly.

    I have 3 projects:
    - Service library with simple service contract and the implementation
    - ServiceHost: Hosting the service library via SelfHosting
    - Client: Consuming a method from the WCF service

    Something I immediately noticed that the Service library had Any CPU as platform . The other two had x86 as platform .
    At this point I had no idea I would get this error, so this didn't really bother me.

    Then I started the service library using the built in wcfsvchost.exe. Service was hosted without a problem.
    Then I wanted to host my service via my own host. Here I got my first experience with System.BadImageFormatException. I had no idea what it was at that point.
    So I was stuck and did some googling. If I understand correctly the platform of all the dlls etc. need to be the same.  if someone can explain me a little better. Please do.

    Anyway so I though I would change the platform of the service library because this had Any CPU instead of x86. So I tried to do it via the Project settings and the configuration manager. But the problem is I had not the option to change it. Then I unloaded the project, looked what the other .csproj looked like it and changed the values there. Reloaded it and everything was fine. Now it had the correct platform, x86 . Ok so let's test run the service library..BOOM System.BadImageFormatException

    System.BadImageFormatException: Could not load file or assembly 'file:///C:\Users\Sven\documents\visual studio 2010\Projects\DuplexDemo\CustomerService2\bin\Debug\CustomerService2.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.
    File name: 'file:///C:\Users\Sven\documents\visual studio 2010\Projects\DuplexDemo\CustomerService2\bin\Debug\CustomerService2.dll'
       at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
       at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection, Boolean suppressSecurityChecks)
       at System.Reflection.Assembly.Load(AssemblyName assemblyRef)
       at Microsoft.Tools.SvcHost.ServiceHostHelper.LoadServiceAssembly(String svcAssemblyPath)

    === Pre-bind state information ===
    LOG: User = Sven-PC\Sven
    LOG: Where-ref bind. Location = C:\Users\Sven\documents\visual studio 2010\Projects\DuplexDemo\CustomerService2\bin\Debug\CustomerService2.dll
    LOG: Appbase = file:///C:/Users/Sven/documents/visual studio 2010/Projects/DuplexDemo/CustomerService2/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: C:\Users\Sven\documents\visual studio 2010\Projects\DuplexDemo\CustomerService2\bin\Debug\CustomerService2.dll.config
    LOG: Using host configuration file:
    LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.21006\config\machine.config.
    LOG: Attempting download of new URL file:///C:/Users/Sven/documents/visual studio 2010/Projects/DuplexDemo/CustomerService2/bin/Debug/CustomerService2.dll.
    ERR: Failed to complete setup of assembly (hr = 0x8007000b). Probing terminated.

    So now that I had changed the platform the service library wouldn't run anymore. So now I'm stuck and I seek some solutions.

    Can anyone tell me what's going on?
    Don't think the code is having errors because it's all plain vanilla WCF and before it was working.

    Please help me out

    Visit my blog for more interesting WCF topics: http://www.mshelp.be
    Sunday, February 07, 2010 11:27 PM

All replies

  • C:\Windows\Microsoft.NET\Framework64\v4.0.21006\config\machine.config.


    Your service host is using the 64bit framework.  x86 won't run in it.

    • Proposed as answer by CassC Friday, March 11, 2011 1:32 AM
    Thursday, June 17, 2010 7:58 PM
  • I had this same issue.  I resolved it be setting the 'Platform target' on the Build tab to 'Any CPU' (for all the projects).  That is different from the 'Platform' drop down at the top of the properties window.


    Hope this helps someone else out there...

    • Proposed as answer by Jacob Appleton Wednesday, September 21, 2011 7:49 AM
    Wednesday, November 17, 2010 5:30 PM
  • I had this same issue.  I resolved it be setting the 'Platform target' on the Build tab to 'Any CPU' (for all the projects).  That is different from the 'Platform' drop down at the top of the properties window.


    Hope this helps someone else out there...

    Bingo! Thanks!
    Cass Cunningham Senior Staff Engineer Western Digital
    Friday, March 11, 2011 1:31 AM
  • But then you cannot edit code when debugging. :(
    Thursday, October 06, 2011 2:49 PM