Mixed mode assembly error fix while using K2 Assemblies

Helooo Every one

Among the articles that I have posted at the beginning of my blog UnCode K2, one of the article explains about File Load Exception which occurs when you are referencing a .Net Assembly built on Version 2, with in a project which is being built on Version 4 or above.

fileloadexception

More or less all source code assemblies which we get from K2 were built on .Net Framework Version 2 and when we use them in our version 4 projects we would encounter this issue.

As a work around we have to add some additional configuration settings in your app.config file to get around this issue.

<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true"> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
</configuration>

 

So basically while you run your application, above mentioned configuration inform .Net runtime to use mixed mode CLR 2 assemblies in a .NET 4 application so that they would load without any issue.

I thought above mentioned configuration will work in all cases, but actually it’s not.

Recently I was working on creating some Custom WCF Services which has dependencies on custom assemblies in which they have referredK2 source code assemblies. Now I’m able to build the project without any issue, but at the runtime I have encountered the above mentioned issue when I’m calling those methods. I tried using above mentioned configuration in WCF Service App Config file but it didn’t work.

After some research on this, I found that WCF Services are creating its config at runtime and it’s not picking up the configuration changes that I have set. So as a workaround for this issue we need to set these settings dynamically via Code.

Below is the class file which imports the current runtime as an interface object and set the legacy v2 runtime and returns true or false based on success.

    public static class ActivateLegacyRuntime
    {
        public static bool SetLegacyV2Runtime()
        {
            ICLRRuntimeInfo objICLRRuntimeInfo =
                (ICLRRuntimeInfo)RuntimeEnvironment.GetRuntimeInterfaceAsObject(
                   Guid.Empty,
                    typeof(ICLRRuntimeInfo).GUID);
            try
            {
                objICLRRuntimeInfo.BindAsLegacyV2Runtime();
                return true;
            }
            catch (COMException)
            {
                return 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();
        }
    }

 

Now once we have our class file ready, we just have to call the method SetLegacyV2Runtime and based on what it returns we need to call the Custom assembly method.

If(ActivateLegacyRuntime.SetLegacyV2Runtime)
{
   //Call the assembly Method
}
else
{
  //Show message cannot load the assembly
}

This have worked for me. Hope it works for you too.

Thanks to authors for these articles at below links which helped me in resolving this thing.

http://reedcopsey.com/2011/09/15/setting-uselegacyv2runtimeactivationpolicy-at-runtime/

https://k2recipes.wordpress.com/2017/07/28/mixed-mode-assembly-is-built-against-version-of-the-runtime-cannot-be-loaded/

 

 

Author: Vijay

I'm a K2 Certified Developer, helping teams to automate, design, develop & maintain business process applications connecting across multiple platforms with multiple technologies.

Leave a comment

Bagi2info.com

Tips dan Trik Komputer, Tutorial Pemrograman, SQL, PHP, React Native - Expo

Mohamed Ashiq Faleel

Its time to think about Microsoft 365

Common Man Tips for Power Platform, Dynamics CRM,Azure

Venkata Subbarao Polisetty - Microsoft MVP,C# Corner MVP

James K2 Blog

Tips and Tricks about K2 workflow software

the BPM freak !!

"The illiterate of the 21st century will not be those who cannot read and write, but those who cannot learn, unlearn, and relearn" – Alvin Toffle

Start Rule

Learning K2 blackpearl and K2 Five

K2Ranger

Do K2 like a BOSS!!

Maverick

discover your own way and lead the world... with Divya Raj

jeylabs

Work Intelligently

Chasing Time - Mobile Gaming Photography Tips Tricks

#iamtheiosphotographer Pro Mobile Game Reviews, Tips, Tri

Jozsef Torsan

Founder & Software Engineer - Creator of the Bookmark Ninja Online Bookmark Manager

GreenEggs on K2

Saving the world with K2