示例#1
0
 public bool                     load(string fullAssemblyName, string pathToAssemblyToLoad, bool copyToAppDomainBaseDirectoryBeforeLoad)
 {
     try
     {
         // copy if asked to
         if (copyToAppDomainBaseDirectoryBeforeLoad)
         {
             try
             {
                 if (File.Exists(pathToAssemblyToLoad))
                 {
                     O2Kernel_Files.Copy(pathToAssemblyToLoad, AppDomain.BaseDirectory);
                 }
                 else
                 {
                     PublicDI.log.error(
                         "copyToAppDomainBaseDirectoryBeforeLoad was set but pathToAssemblyToLoad was set to a file that didn't exist: {0}",
                         pathToAssemblyToLoad);
                 }
             }
             catch (Exception ex)
             {
                 ex.log("in load copyToAppDomainBaseDirectoryBeforeLoad");
             }
         }
         // load assembly into AppDomain
         //First try directly
         AppDomain.Load(fullAssemblyName);
     }
     catch (Exception ex1)
     {
         //then try using AssemblyName
         try
         {
             AppDomain.Load(AssemblyName.GetAssemblyName(fullAssemblyName));
         }
         catch (Exception ex2)
         {
             // last change load assembly into current appdomain to get its full name and try again
             try
             {
                 AppDomain.Load(fullAssemblyName.assembly().FullName);
             }
             catch (Exception ex3)
             {
                 PublicDI.log.ex(ex1, "could not load assembly (method1): " + fullAssemblyName);
                 PublicDI.log.ex(ex2, "could not load assembly (method2): " + fullAssemblyName);
                 PublicDI.log.ex(ex3, "could not load assembly (method3): " + fullAssemblyName);
                 return(false);
             }
         }
     }
     return(true);
 }
示例#2
0
        /*static Assembly assemblyResolve(object sender, ResolveEventArgs args)
         * {
         *
         *  //return Assembly.LoadFile(GetAssemblyFileName());
         *  return null;
         * } */

        public void loadAssembliesIntoAppDomain(List <string> assembliesToLoad)
        {
            var assembliesInNewAppDomain = new List <String>();

            // first copy the assemblies
            foreach (var assemblyToLoad in assembliesToLoad)
            {
                if (Path.GetExtension(assemblyToLoad).ToLower() == ".dll" || Path.GetExtension(assemblyToLoad).ToLower() == ".exe") // since other wise these might be GAC assemblies
                {
                    if (File.Exists(assemblyToLoad))
                    {
                        var targetFileName = Path.Combine(AppDomain.BaseDirectory, assemblyToLoad.fileName());
                        if (targetFileName.fileExists().isFalse())
                        {
                            O2Kernel_Files.Copy(assemblyToLoad, targetFileName);
                        }
                        assembliesInNewAppDomain.Add(targetFileName);
                    }
                    else
                    {
                        var resolvedName = Path.Combine(PublicDI.config.CurrentExecutableDirectory, assemblyToLoad);
                        if (File.Exists(resolvedName))
                        {
                            var targetFileName = Path.Combine(AppDomain.BaseDirectory, resolvedName.fileName());
                            if (targetFileName.fileExists().isFalse())
                            {
                                O2Kernel_Files.Copy(resolvedName, targetFileName);
                            }
                            assembliesInNewAppDomain.Add(targetFileName);
                        }
                        else
                        {
                            PublicDI.log.error("in loadAssembliesIntoAppDomain , could not find dll to copy: {0}",
                                               assemblyToLoad);
                        }
                    }
                }
            }
            // then load them (and if there are no missing dependencies ALL should load ok
            foreach (var assemblyToLoad in assembliesInNewAppDomain)
            {
                try
                {
                    AppDomain.Load(Path.GetFileNameWithoutExtension(assemblyToLoad));
                }
                catch (Exception ex)
                {
                    PublicDI.log.ex(ex,
                                    "in O2AppDomainFactory.loadAssembliesIntoAppDomain, could not load assembly: " +
                                    assemblyToLoad);
                }
            }
        }