【问题标题】:Mixed mode assembly cannot be loaded无法加载混合模式程序集
【发布时间】:2015-06-09 08:15:18
【问题描述】:

我的所有项目都设置为使用 .net 4.5.1 测试项目使用一些面向 .NET v2.0.50727 的 SQl Server 程序集。

App.config:

<startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime  version="v2.0.50727"/>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>   
</startup>

我还检查了这个线程: Mixed mode assembly

【问题讨论】:

  • 对于所有投反对票的人,最好提供解决方案而不是投反对票!或在您投反对票时按照建议发表评论!
  • (我不是反对者。) 大概 app.config 被复制到构建输出文件夹并且(比如你的启动程序集是ass.exe)呈现为@ 987654324@.
  • 设置为“不复制”,只有 exe.config 在 bin 中并按照上述配置....hm。出现错误的项目是一个c#测试项目。访问 Microsoft.SqlServer*.dll 的 api 代码时会发生这种情况。它们是在 .NET v2.0.50727 上编译的。

标签: .net


【解决方案1】:

由于启动元素不适合我,我采用了以下方法:

Detailed explanation

使用以下帮助类:

using System;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;

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

在调用 SMO 之前,请检查一切是否正常

if (!RuntimePolicyHelper.LegacyV2RuntimeEnabledSuccessfully)
                throw new Exception("Could not load SMO");

【讨论】:

    猜你喜欢
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 2011-03-20
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 2012-10-06
    相关资源
    最近更新 更多