【问题标题】:How to load a .dll that was build for another .net version in .Net 5?如何在 .Net 5 中加载为另一个 .net 版本构建的 .dll?
【发布时间】:2015-12-15 08:40:32
【问题描述】:

在以前的 ASP.NET 版本(直到 4.6 版)中,我们可以通过如下修改 web.config 来加载为另一个 .net 版本构建的 *.dll:

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

但是在 ASP.NET 5 中,没有 web.config,而是完全不同的配置系统。那么新版本如何才能得到同样的结果呢?

【问题讨论】:

  • 您好,欢迎来到 SO,请在您的标题中提供问题的摘要(不仅仅是技术列表)。通常应使用标签列出技术

标签: asp.net asp.net-mvc asp.net-core


【解决方案1】:

这篇博文展示了如何在运行时设置此策略(与“设计时”相比 - 通过编辑 web.config)http://reedcopsey.com/2011/09/15/setting-uselegacyv2runtimeactivationpolicy-at-runtime/,但我自己还没有尝试使用 ASP.NET 5。虽然在早期版本上工作过。

基本上你创建这个静态帮助类

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();
    }
}

用法:

// before calling the code from your legacy assembly - 
if (RuntimePolicyHelper.LegacyV2RuntimeEnabledSuccessfully)
{
    // your Legacy code
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-04
    相关资源
    最近更新 更多