Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load a .dll that was build for another .net version in .Net 5?

In previous versions of ASP.NET (till ver 4.6), we can load a *.dll that was build for another .net version by modifying the web.config as follow:

<configuration>
     <startup useLegacyV2RuntimeActivationPolicy="true" >   
     </startup>
</configuration>

But in ASP.NET 5, there is no web.config, but a completely different configuration system. So how can we get the same result with the new version?

like image 555
Mohamed Lotfy Avatar asked Dec 15 '15 08:12

Mohamed Lotfy


People also ask

Can .NET 5 use .NET framework libraries?

In this articleNET Framework libraries aren't available for use with . NET 5+ (and . NET Core), such as app domains, remoting, and code access security (CAS).

How does .NET determine DLL dependencies?

Load your DLL into it, right click, and chose 'Analyze' - you'll then see a "Depends On" item which will show you all the other dll's (and methods inside those dll's) that it needs.

Can you have two versions of .NET installed?

Microsoft designed the . NET Framework so that multiple versions of the framework can be installed and used at the same time. This means that there will be no conflict if multiple applications install different versions of the . NET framework on a single computer.


1 Answers

This blog post shows how to set this policy at runtime (vs. the "design-time" - by editing web.config) http://reedcopsey.com/2011/09/15/setting-uselegacyv2runtimeactivationpolicy-at-runtime/ but I havent' tried it with ASP.NET 5 myself. Worked on earlier versions though.

Basically you create this static helper class

public static class RuntimePolicyHelper
{
    public static bool LegacyV2RuntimeEnabledSuccessfully { get; private set; }

    static RuntimePolicyHelper()
    {
        ICLRRuntimeInfo clrRuntimeInfo =
            (ICLRRuntimeInfo)RuntimeEnvironment.GetRuntimeInterfaceAsObject(
                Guid.Empty, 
                typeof(ICLRRuntimeInfo).GUID);
        try
        {
            clrRuntimeInfo.BindAsLegacyV2Runtime();
            LegacyV2RuntimeEnabledSuccessfully = true;
        }
        catch (COMException)
        {
            // This occurs with an HRESULT meaning 
            // "A different runtime was already bound to the legacy CLR version 2 activation policy."
            LegacyV2RuntimeEnabledSuccessfully = false;
        }
    }

    [ComImport]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid("BD39D1D2-BA2F-486A-89B0-B4B0CB466891")]
    private 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();
    }
}

Usage:

// before calling the code from your legacy assembly - 
if (RuntimePolicyHelper.LegacyV2RuntimeEnabledSuccessfully)
{
    // your Legacy code
}
like image 156
jazzcat Avatar answered Sep 27 '22 18:09

jazzcat