How to pre-load all referenced assemblies

Add-in Express™ Support Service
That's what is more important than anything else

How to pre-load all referenced assemblies
 
Abhijeet S




Posts: 12
Joined: 2017-03-27
Product : Add-in Express for Office and .NET (ver. Addin Express version 8.6.4408 )
OS : Windows 10
Host application : Office 2016
IDE : Visual Studio 2015

I have a code snippet ready which preloads all referenced assemblies, Question is where to hook it up?

        private static IEnumerable<string> GetBinFolders()
        {
            List<string> toReturn = new List<string>();
            toReturn.Add(AppDomain.CurrentDomain.BaseDirectory);
            return toReturn;
        }

        private static void PreLoadAssembliesFromPath(string p)
        {
            FileInfo[] files = null;
            files = new DirectoryInfo(p).GetFiles("*.dll", SearchOption.AllDirectories);

            AssemblyName a = null;
            string s = null;
            foreach (var fi in files)
            {
                s = fi.FullName;
                a = AssemblyName.GetAssemblyName(s);
                if (!AppDomain.CurrentDomain.GetAssemblies().Any(assembly => AssemblyName.ReferenceMatchesDefinition(a, assembly.GetName())))
                {
                    Assembly.Load(a);
                }
            }
        }

        private static void PreLoadDeployedAssemblies()
        {
            foreach (var path in GetBinFolders())
            {
                PreLoadAssembliesFromPath(path);
            }
        }


AddinModule.cs constructor?? I tried that, now project output dll couldn't be registered.

C:Program Files (x86)MSBuild.0inMicrosoft.Common.CurrentVersion.targets(4335,5): error MSB3217: Cannot register assembly "output.dll". Exception has been thrown by the target of an invocation.


Please suggest.
Posted 21 Apr, 2017 02:50:21 Top
Andrei Smolin


Add-in Express team


Posts: 14129
Joined: 2006-05-11
Hello Abhijeet,

You should do this in the very first Office event. Depending on the host application, its version, and the way you start it, the very first event would be either AddinInitialize or OnribbonBeforeCreate; see events of the add-in module. Doing this also requires that you initialize all the variables of the types defined in these assemblies in that event, after you preload the assemblies. Accordingly, if any of such variables is defined on the class level of the add-in module, you should declare them non-initialized; any initializer will load the assembly containing the type.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 21 Apr, 2017 05:02:47 Top