0

我在 scriptcs 中添加了一个应用程序,并添加了一些对版本为 v2.0.50727 的程序集的引用。因此,在运行 scriptcs 文件时,它返回的混合模式程序集是针对运行时的版本“v2.0.50727”构建的,并且无法在 4.0 运行时中加载。在 app.config 中设置属性 useLegacyV2RuntimeActivationPolicy="true" 可能会解决 asp 中的问题.net 网络应用程序。但在脚本中它不起作用。进一步搜索发现上述属性 useLegacyV2RuntimeActivationPolicy="true" 应该添加为 scriptcs.exe.config。我有一个名为 FMUpgrade.csx 的应用程序文件,我们如何在 FMUpgrade.csx 文件中引用这个 scriptcs.exe.config。scriptcs 文档对 scriptcs.exe.config 并没有说太多。还添加了带有 app 的 program.exe.config .config 但仍然没有成功。

4

1 回答 1

0

经过大量研究,我得到了解决上述问题的解决方法。通过使用类 ExeConfigurationFileMap 我们可以从 app.config 中获取键值,它无法绕过由混合模式组装错误导致的支持的运行时错误。服务器 server = new Server(new ServerConnection(con)); server.ConnectionContext.ExecuteNonQuery(脚本); 该错误是在执行语句 ExecuteNonQuery 时引起的。所以在执行语句之前

if(RuntimePolicyHelper.LegacyV2RuntimeEnabledSuccessfully) server.ConnectionContext.ExecuteNonQuery(script);

下面是使用 System.Runtime.CompilerServices 的解决方案;使用 System.Runtime.InteropServices;公共静态类 RuntimePolicyHelper { 公共静态 bool LegacyV2RuntimeEnabledSuccessfully { 获取;私人套装;}

    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();
    }
 } using System.Runtime.CompilerServices;

使用 System.Runtime.InteropServices;公共静态类 RuntimePolicyHelper { 公共静态 bool LegacyV2RuntimeEnabledSuccessfully { 获取;私人套装;}

    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();
    }
 }
于 2016-06-28T05:16:16.903 回答