【问题标题】:scriptcs Mixed mode assembly errorscriptcs 混合模式组装错误
【发布时间】:2016-10-19 02:26:48
【问题描述】:

我在 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 但仍然没有成功。

【问题讨论】:

    标签: scriptcs


    【解决方案1】:

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

    if(RuntimePolicyHelper.LegacyV2RuntimeEnabledSuccessfully) server.ConnectionContext.ExecuteNonQuery(脚本);

    解决方案如下 使用 System.Runtime.CompilerServices; 使用 System.Runtime.InteropServices; 公共静态类 RuntimePolicyHelper { 公共静态布尔 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 { 公共静态布尔 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();
        }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-25
      • 2011-04-14
      • 2015-08-09
      • 1970-01-01
      • 1970-01-01
      • 2017-03-04
      • 2012-12-28
      • 2012-10-21
      相关资源
      最近更新 更多