locked
XBAP: Mixed mode assembly unable to load in .net 4.0 RRS feed

  • Question

  • Hi!

    I've followed the advice on loading assemblies compiled under .NET 2.0 in a .NET 4.0 application, namely to add this element to the application configuration file:
    <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0"/>
      </startup>

    My question is how to go about this in an WPF XBAP compiled .NET 4.0 application. I've tried adding a configuration file with this element, but I still get this exception:

    Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.

    Any advice?
    Wednesday, March 10, 2010 12:00 PM

All replies

  • Which version of .NET 4 do you use? RC, Beta2, something else? (If you are not sure, please post name of directory %WinDir%\Microsoft.NET\Framework\v4.0.*)

    Try to create a small repro for that. I tried this on 4.0 RC and it works for me:

    ; File: Library.def
    EXPORTS
        PrintHelloWorld
    
    // Library.cpp
    // Compile: cl.exe /LD /clr Library.cpp /link /def:Library.def
    void ManagedFunction() {
        System::Console::WriteLine(L"{0}", System::Environment::Version);
    }
    
    #pragma managed(push, off)
    extern "C" {
        void PrintHelloWorld() {
            ManagedFunction();
        }
    }
    #pragma managed(pop)
    
    // HelloWorld.cpp
    // Compile: cl.exe HelloWorld.cpp Library.lib
    extern "C" {
        void PrintHelloWorld();
    }
    void main() {
        PrintHelloWorld();
    }
    
    <!-- HelloWorld.exe.config -->
    <configuration>
        <startup useLegacyV2RuntimeActivationPolicy="true">
            <supportedRuntime version="v4.0" />
        </startup>
    </configuration>
    

    -Karel
    Thursday, March 11, 2010 2:30 AM
  • Hi Karel

    Thanks for your response. I mostly use C# and don't work much in c++, so I could be wrong in reading your example, but did you compile it to a WPF XBAP application and run it in IE?

    The config element works for a normal compiled WPF application but I can't get it working when compiled as an XBAP.
    Thursday, March 11, 2010 9:25 AM
  • Hi Karel

    Yes it is indeed the Release Candidate. Did you run your application as an XBAP browser hosted application?
    Thursday, March 11, 2010 3:56 PM
  • No I didn't try it in XBAP in IE - I don't know how to do that.
    It is quite possible that XBAP apps have totally different rules for loading runtime and config file settings.
    This is question probably for WPF, I'll move the thread.

    -Karel
    Thursday, March 11, 2010 4:52 PM
  • We are running into the same exact problem - .NET 4.0 XBAP application which uses SlimDX causes this error.  I note the following documentation:
    http://msdn.microsoft.com/en-us/library/bbx34a2h%28VS.100%29.aspx
    "The startup code for an application hosted in Microsoft Internet Explorer ignores the <startup> element and its child elements."

    So, how then can we set this attribute for an XBAP?
    Monday, March 15, 2010 7:58 PM
  • [ComImport]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid("BD39D1D2-BA2F-486A-89B0-B4B0CB466891")]
    
    interface ICLRRuntimeInfo {
    
    void xGetVersionString();
    void xGetRuntimeDirectory();
    void xIsLoaded();
    void xIsLoadable();
    void xLoadErrorString();
    void xLoadLibrary();
    void xGetProcAddress();
    void xGetInterface();
    void xSetDefaultStartupFlags();
    void xGetDefaultStartupFlags();
    
    [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
    
    void BindAsLegacyV2Runtime();
    
    }
    and then you can do this: ICLRRuntimeInfo rtInfo = (ICLRRuntimeInfo)RuntimeEnvironment.GetRuntimeInterfaceAsObject(Guid.Empty, typeof(ICLRRuntimeInfo).GUID); rtInfo.BindAsLegacyV2Runtime(); Took me a long time to figure out! Hope this helps.
    • Proposed as answer by Chris Chau Tuesday, June 22, 2010 6:58 PM
    Tuesday, June 22, 2010 6:56 PM
  • I think that this solution need recompilation of application through the .net framework 4.0. In .net 3.5 SP1, I can't find the specified method from RuntimeEnvironment, so is there any simpler solution. What is the execution path of xbap in browser.
    Moattar
    Saturday, October 30, 2010 2:53 PM
  • I think that this solution need recompilation of application through the .net framework 4.0. In .net 3.5 SP1, I can't find the specified method from RuntimeEnvironment, so is there any simpler solution. What is the execution path of xbap in browser.
    Moattar

    You shouldn't need this solution if you are in .NET 3.5 SP1. This problem exists when you have a .NET 4.0 XBAP that tries to load a .NET 2.0 (3.5) assembly.

    Tuesday, January 25, 2011 2:34 PM
  • Great work Chris,

    you made my day.

    Thank you very much, this works like a charm.

    Cheers

    Andreas Bender

    Thursday, January 27, 2011 2:16 PM
  • Seems ridiculous that this is required to allow XBAP's to load prior versions.  Many thanks to Chris for figuring this one out.  Saved me loads of time.
    Friday, February 18, 2011 4:48 PM