简体   繁体   English

脚本混合模式汇编错误

[英]scriptcs Mixed mode assembly error

I have added an application in scriptcs and added some references to assemblies which have the version v2.0.50727. 我在脚本中添加了一个应用程序,并添加了对版本为v2.0.50727的程序集的一些引用。 So while running the scriptcs file it returns as Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime.Setting attribute useLegacyV2RuntimeActivationPolicy="true" in app.config may resolve the issue in asp.net web application. 因此,在运行scriptcs文件时,它将根据运行时版本“ v2.0.50727”构建混合模式程序集,并且无法在4.0运行时中加载。app.config中的设置属性useLegacyV2RuntimeActivationPolicy =“ true”可以解决ASP中的问题.net Web应用程序。 but in scriptcs its not working. 但在脚本中它不起作用。 further searching reveals that above attribbute useLegacyV2RuntimeActivationPolicy="true" should be added as scriptcs.exe.config. 进一步的搜索显示,以上属性useLegacyV2RuntimeActivationPolicy =“ true”应该作为scriptcs.exe.config添加。 I have an application file named FMUpgrade.csx and how can we reference this scriptcs.exe.config in the FMUpgrade.csx file.scriptcs docs doesn't say much about scriptcs.exe.config.Also added program.exe.config with app.config but still not success. 我有一个名为FMUpgrade.csx的应用程序文件,我们如何在FMUpgrade.csx文件中引用此scriptcs.exe.config。scriptcs文档对scriptcs.exe.config的描述并不多。 .config,但仍然没有成功。

After much research I got a workaround solution to the above problem. 经过大量研究,我找到了解决上述问题的解决方法。 By make use of class ExeConfigurationFileMap we could able to get the key values from app.config, its not able to bypass the supported runtime error caused by mixed mode assembly error. 通过使用ExeConfigurationFileMap类,我们可以从app.config获取键值,它无法绕过由混合模式汇编错误引起的受支持的运行时错误。 Server server = new Server(new ServerConnection(con)); 服务器服务器=新服务器(new ServerConnection(con)); server.ConnectionContext.ExecuteNonQuery(script); server.ConnectionContext.ExecuteNonQuery(script); The error is caused while executing the statement ExecuteNonQuery. 该错误是在执行语句ExecuteNonQuery时引起的。 So before executing the statement 所以在执行语句之前

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

Solution is below using System.Runtime.CompilerServices; 下面是使用System.Runtime.CompilerServices的解决方案; using System.Runtime.InteropServices; 使用System.Runtime.InteropServices; public static class RuntimePolicyHelper { public static bool LegacyV2RuntimeEnabledSuccessfully { get; 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();
    }
 } using System.Runtime.CompilerServices;

using System.Runtime.InteropServices; 使用System.Runtime.InteropServices; public static class RuntimePolicyHelper { public static bool LegacyV2RuntimeEnabledSuccessfully { get; 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();
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM