-1

All my projects are setup to use .net 4.5.1 The test project uses some SQl Server assemblies targeting.NET v2.0.50727.

App.config:

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

I also checked this thread among others: Mixed mode assembly

Community
  • 1
  • 1
Legends
  • 21,202
  • 16
  • 97
  • 123
  • 1
    For all who downvoted, better provide a solution instead of downvoting! Or leave a comment as suggested, when you downvote! – Legends Apr 04 '15 at 12:18
  • (*I'm not a downvoter.*) Presumably the app.config is copied to the build output folder and (say your startup assembly is `ass.exe`) present as `ass.exe.config`. – Wai Ha Lee Apr 04 '15 at 15:52
  • It's set to "Do not copy", only the exe.config is in the bin and configured as above mentioned....hm. The project in which the error occurs is a c# test project. It happens when api code from Microsoft.SqlServer*.dll is accessed. Theay are compiled on .NET v2.0.50727. – Legends Apr 04 '15 at 16:38

1 Answers1

0

As the startup element didn't do it for me, I went along with the following approach:

Detailed explanation

Use the following helper class:

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

And before you call the SMO stuff, just check that everything is ok

if (!RuntimePolicyHelper.LegacyV2RuntimeEnabledSuccessfully)
                throw new Exception("Could not load SMO");
Legends
  • 21,202
  • 16
  • 97
  • 123