private Assembly? CurrentDomain_AssemblyResolve()

in src/Microsoft.VisualStudio.Composition.AppHost/CreateComposition.cs [314:390]


        private Assembly? CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            // apply any existing policy
            AssemblyName referenceName = new AssemblyName(AppDomain.CurrentDomain.ApplyPolicy(args.Name));

            string fileName = referenceName.Name + ".dll";
            Assembly? assm;

            // Look through user-specified assembly lists.
            foreach (var candidate in this.catalogAssemblyPaths.Concat(this.ReferenceAssemblies.Select(i => i.GetMetadata("FullPath"))))
            {
                if (string.Equals(referenceName.Name, Path.GetFileNameWithoutExtension(candidate), StringComparison.OrdinalIgnoreCase))
                {
                    if (this.Probe(candidate, referenceName.Version, out assm))
                    {
                        return assm;
                    }
                }
            }

            string probingPath;
            foreach (var searchDir in this.CatalogAssemblySearchPath)
            {
                probingPath = Path.Combine(searchDir.GetMetadata("FullPath"), fileName);
                Debug.WriteLine($"Considering {probingPath} based on catalog assembly search path");
                if (this.Probe(probingPath, referenceName.Version, out assm))
                {
                    return assm;
                }
            }

            // look next to requesting assembly
            string? assemblyPath = args.RequestingAssembly?.Location;
            if (!string.IsNullOrEmpty(assemblyPath))
            {
                probingPath = Path.Combine(Path.GetDirectoryName(assemblyPath), fileName);
                Debug.WriteLine($"Considering {probingPath} based on RequestingAssembly");
                if (this.Probe(probingPath, referenceName.Version, out assm))
                {
                    return assm;
                }
            }

            // look next to the executing assembly
            assemblyPath = Assembly.GetExecutingAssembly().Location;
            if (!string.IsNullOrEmpty(assemblyPath))
            {
                probingPath = Path.Combine(Path.GetDirectoryName(assemblyPath), fileName);

                Debug.WriteLine($"Considering {probingPath} based on ExecutingAssembly");
                if (this.Probe(probingPath, referenceName.Version, out assm))
                {
                    return assm;
                }
            }

            // look in AppDomain base directory
            probingPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
            Debug.WriteLine($"Considering {probingPath} based on BaseDirectory");
            if (this.Probe(probingPath, referenceName.Version, out assm))
            {
                return assm;
            }

            bool shouldLog;
            lock (this.loggedMissingAssemblyNames)
            {
                shouldLog = this.loggedMissingAssemblyNames.Add(args.Name);
            }

            if (shouldLog)
            {
                this.Log.LogMessage("\"{0}\" could not be found.", args.Name);
            }

            return null;
        }