private static AssemblyResolutionResult ResolveAssembly(string assemblyName, IList <string> searchPaths, IList <string> targetFrameworkDirectories, bool checkDependencies)
        {
            ResolveAssemblyReference reference = new ResolveAssemblyReference();
            MockEngine engine = new MockEngine();

            reference.BuildEngine = engine;
            if (searchPaths != null)
            {
                reference.SearchPaths = searchPaths.ToArray <string>();
            }
            if (targetFrameworkDirectories != null)
            {
                reference.TargetFrameworkDirectories = targetFrameworkDirectories.ToArray <string>();
            }
            reference.Assemblies = new ITaskItem[] { new TaskItem(assemblyName) };
            reference.Silent     = true;
            reference.Execute();
            AssemblyResolutionResult result = new AssemblyResolutionResult();
            List <string>            list   = new List <string>();

            foreach (ITaskItem item in reference.ResolvedFiles)
            {
                list.Add(item.ItemSpec);
            }
            if (checkDependencies)
            {
                CheckOutOfRangeDependencies(assemblyName);
            }
            result.ResolvedFiles = list.ToArray();
            result.Warnings      = engine.Warnings;
            result.Errors        = engine.Errors;
            return(result);
        }
        internal static ReferenceAssemblyType GetPathToReferenceAssembly(Assembly a, out string path, ICollection <BuildErrorEventArgs> errors, ICollection <BuildWarningEventArgs> warnings, bool checkDependencies)
        {
            lock (s_lock)
            {
                if (AssemblyLocations.TryGetValue(a, out path))
                {
                    return(ReferenceAssemblyTypes[a]);
                }
            }
            if ((TargetFrameworkReferenceAssemblyPaths == null) || (TargetFrameworkReferenceAssemblyPaths.Count == 0))
            {
                path = Util.GetAssemblyCodeBase(a);
                return(ReferenceAssemblyType.FrameworkAssembly);
            }
            AssemblyResolutionResult result = null;
            ReferenceAssemblyType    nonFrameworkAssembly = ReferenceAssemblyType.NonFrameworkAssembly;

            if (BuildResultCompiledAssemblyBase.AssemblyIsInCodegenDir(a))
            {
                path = Util.GetAssemblyCodeBase(a);
            }
            else
            {
                nonFrameworkAssembly = GetPathToReferenceAssembly(a, out path, errors, warnings, checkDependencies, true, out result);
            }
            StoreResults(a, path, result, nonFrameworkAssembly);
            return(nonFrameworkAssembly);
        }
 private static void StoreResults(Assembly a, string path, AssemblyResolutionResult result, ReferenceAssemblyType assemblyType)
 {
     lock (s_lock) {
         if (!AssemblyLocations.ContainsKey(a))
         {
             AssemblyLocations.Add(a, path);
             AssemblyResolutionResults.Add(a, result);
             ReferenceAssemblyTypes.Add(a, assemblyType);
         }
     }
 }
        internal static ReferenceAssemblyType GetPathToReferenceAssembly(Assembly a, out string path,
                                                                         ICollection <BuildErrorEventArgs> errors, ICollection <BuildWarningEventArgs> warnings,
                                                                         bool checkDependencies)
        {
            lock (s_lock) {
                if (AssemblyLocations.TryGetValue(a, out path))
                {
                    return(ReferenceAssemblyTypes[a]);
                }
            }

            // If there are no reference assemblies available, just use the path to the loaded assembly.
            if (TargetFrameworkReferenceAssemblyPaths == null || TargetFrameworkReferenceAssemblyPaths.Count == 0)
            {
                path = System.Web.UI.Util.GetAssemblyCodeBase(a);
                return(ReferenceAssemblyType.FrameworkAssembly);
            }

            AssemblyResolutionResult result = null;
            ReferenceAssemblyType    referenceAssemblyType = ReferenceAssemblyType.NonFrameworkAssembly;

            // If the assembly is generated by us, it is a non framework assembly and does not need to be resolved.
            if (BuildResultCompiledAssemblyBase.AssemblyIsInCodegenDir(a))
            {
                path = System.Web.UI.Util.GetAssemblyCodeBase(a);
            }
            else
            {
                // Try using the assembly full name.
                referenceAssemblyType = GetPathToReferenceAssembly(a, out path, errors, warnings,
                                                                   checkDependencies, true /*useFullName*/, out result);
            }

            StoreResults(a, path, result, referenceAssemblyType);
            return(referenceAssemblyType);
        }
        private static ReferenceAssemblyType GetPathToReferenceAssembly(Assembly a, out string path, ICollection <BuildErrorEventArgs> errors, ICollection <BuildWarningEventArgs> warnings, bool checkDependencies, bool useFullName, out AssemblyResolutionResult result)
        {
            string originalAssemblyName;
            string name = a.GetName().Name;

            if (useFullName)
            {
                originalAssemblyName = CompilationSection.GetOriginalAssemblyName(a);
            }
            else
            {
                originalAssemblyName = name;
            }
            result = ResolveAssembly(originalAssemblyName, TargetFrameworkReferenceAssemblyPaths, TargetFrameworkReferenceAssemblyPaths, false);
            if ((result.ResolvedFiles != null) && (result.ResolvedFiles.Count > 0))
            {
                path = result.ResolvedFiles.FirstOrDefault <string>();
                FixMscorlibPath(a, ref path);
                return(ReferenceAssemblyType.FrameworkAssembly);
            }
            result = ResolveAssembly(originalAssemblyName, HigherFrameworkReferenceAssemblyPaths, HigherFrameworkReferenceAssemblyPaths, false);
            if ((result.ResolvedFiles != null) && (result.ResolvedFiles.Count > 0))
            {
                path = result.ResolvedFiles.FirstOrDefault <string>();
                return(ReferenceAssemblyType.FrameworkAssemblyOnlyPresentInHigherVersion);
            }
            if (NeedToCheckFullProfile)
            {
                result = ResolveAssembly(originalAssemblyName, FullProfileReferenceAssemblyPaths, FullProfileReferenceAssemblyPaths, false);
                if ((result.ResolvedFiles != null) && (result.ResolvedFiles.Count > 0))
                {
                    path = result.ResolvedFiles.FirstOrDefault <string>();
                    string str3 = "";
                    if (!string.IsNullOrEmpty(MultiTargetingUtil.TargetFrameworkName.Profile))
                    {
                        str3 = " '" + MultiTargetingUtil.TargetFrameworkName.Profile + "'";
                    }
                    ReportWarningOrError(System.Web.SR.GetString("Assembly_not_found_in_profile", new object[] { originalAssemblyName, str3 }));
                    return(ReferenceAssemblyType.FrameworkAssemblyOnlyPresentInHigherVersion);
                }
            }
            List <string> searchPaths = new List <string>();

            searchPaths.AddRange(TargetFrameworkReferenceAssemblyPaths);
            searchPaths.Add(Path.GetDirectoryName(a.Location));
            if (useFullName)
            {
                searchPaths.Add("{GAC}");
            }
            if (!useFullName)
            {
                originalAssemblyName = a.GetName().FullName;
            }
            result = ResolveAssembly(originalAssemblyName, searchPaths, TargetFrameworkReferenceAssemblyPaths, checkDependencies);
            path   = result.ResolvedFiles.FirstOrDefault <string>();
            if (string.IsNullOrEmpty(path))
            {
                path = Util.GetAssemblyCodeBase(a);
            }
            if (useFullName)
            {
                AssemblyResolutionResult result2 = ResolveAssembly(name, HigherFrameworkReferenceAssemblyPaths, HigherFrameworkReferenceAssemblyPaths, false);
                if ((result2.ResolvedFiles != null) && (result2.ResolvedFiles.Count > 0))
                {
                    return(ReferenceAssemblyType.FrameworkAssembly);
                }
            }
            return(ReferenceAssemblyType.NonFrameworkAssembly);
        }
        private static ReferenceAssemblyType GetPathToReferenceAssembly(Assembly a, out string path,
                                                                        ICollection <BuildErrorEventArgs> errors, ICollection <BuildWarningEventArgs> warnings,
                                                                        bool checkDependencies, bool useFullName, out AssemblyResolutionResult result)
        {
            // 1. Find the assembly using RAR in the target framework.
            //    - If found, assembly is a framework assembly. Done
            // 2. Find the assembly using RAR in higher frameworks.
            //    - If found, assembly is a framework assembly only present in a higher version. Done.
            // 3. Find the assembly using RAR in the full profile framework.
            //    - If found, assembly is a framework assembly, but is only present in the full profile framework and not the current target profile. Done.
            // 4. Is useFullName true?
            //    - Yes: Use GAC and directory of loaded assembly as search paths.
            //    - No: Use directory of loaded assembly as search path.
            //    - Use RAR to find assembly in search paths.
            //    - Check for out of range dependencies.
            // 5. If useFullName
            //    - Check if the short name exists in a higher framework, if so, it is a framework assembly.

            // Find the assembly in the target framework.
            string assemblyName;
            string partialName = a.GetName().Name;

            if (useFullName)
            {
                // Use the actual assembly name as specified in the config.
                assemblyName = CompilationSection.GetOriginalAssemblyName(a);
            }
            else
            {
                assemblyName = partialName;
            }
            result = ResolveAssembly(assemblyName, TargetFrameworkReferenceAssemblyPaths,
                                     TargetFrameworkReferenceAssemblyPaths, false /*checkDependencies*/);
            if (result.ResolvedFiles != null && result.ResolvedFiles.Count > 0)
            {
                path = result.ResolvedFiles.FirstOrDefault();
                return(ReferenceAssemblyType.FrameworkAssembly);
            }

            // At this point, the assembly was not found in the target framework.
            // Try finding it in the latest framework.
            result = ResolveAssembly(assemblyName, HigherFrameworkReferenceAssemblyPaths, HigherFrameworkReferenceAssemblyPaths,
                                     false /*checkDependencies*/);
            if (result.ResolvedFiles != null && result.ResolvedFiles.Count > 0)
            {
                path = result.ResolvedFiles.FirstOrDefault();
                // Assembly was found in a target framework of a later version.
                return(ReferenceAssemblyType.FrameworkAssemblyOnlyPresentInHigherVersion);
            }

            // Try to find the assembly in the full profile, in case the user
            // is using an assembly that is not in the target profile framework.
            // For example, System.Web is not present in the Client profile, but is present in the full profile.
            if (NeedToCheckFullProfile)
            {
                result = ResolveAssembly(assemblyName, FullProfileReferenceAssemblyPaths, FullProfileReferenceAssemblyPaths,
                                         false /*checkDependencies*/);
                if (result.ResolvedFiles != null && result.ResolvedFiles.Count > 0)
                {
                    // Assembly was found in the full profile, but not in the target profile.
                    path = result.ResolvedFiles.FirstOrDefault();
                    // Report warning/error message.
                    string profile = "";
                    if (!string.IsNullOrEmpty(MultiTargetingUtil.TargetFrameworkName.Profile))
                    {
                        profile = " '" + MultiTargetingUtil.TargetFrameworkName.Profile + "'";
                    }
                    ReportWarningOrError(SR.GetString(SR.Assembly_not_found_in_profile, assemblyName, profile));
                    // Return as OnlyPresentInHigherVersion so that it will not be used as a reference assembly.
                    return(ReferenceAssemblyType.FrameworkAssemblyOnlyPresentInHigherVersion);
                }
            }

            // Assembly is not found in the framework.
            // Check whether it has any references to assemblies of a higher version.
            List <string> searchPaths = new List <string>();

            searchPaths.AddRange(TargetFrameworkReferenceAssemblyPaths);
            searchPaths.Add(Path.GetDirectoryName(a.Location));
            // If we are using full names, include the GAC so that we can retrieve the actual
            // specified version of an OOB assembly even if it is unified/redirected to a later version.
            // For example, System.Web.Extensions 1.0.61025 is available from the GAC, but the actual
            // loaded assembly is 4.0 due to unification.
            if (useFullName)
            {
                searchPaths.Add("{GAC}");
            }

            // When checking dependencies of a custom assembly, use the full
            // name of the assembly as it might have a strong name or
            // be in the GAC.
            if (!useFullName)
            {
                assemblyName = a.GetName().FullName;
            }
            result = ResolveAssembly(assemblyName, searchPaths, TargetFrameworkReferenceAssemblyPaths, checkDependencies);
            // Use the actual resolved path, in case the loaded assembly is different from the specified assembly
            // due to unification or binding redirect.
            path = result.ResolvedFiles.FirstOrDefault();

            if (string.IsNullOrEmpty(path))
            {
                // In some cases, we might not be able to resolve the path to the assembly successfully, for example when
                // the config specifies the full name as System.Web 4.0.10101.0. Assembly.Load returns the 4.0.0.0 version,
                // but we can't find any actual assembly with such a full name.
                path = System.Web.UI.Util.GetAssemblyCodeBase(a);
            }

            // If we are using full names, do another check using the partial name to see if the assembly is part of
            // a higher framework.
            // If so, then this is an OOB assembly that later got rolled into the framework, so we consider the assembly
            // as a framework assembly.
            if (useFullName)
            {
                AssemblyResolutionResult r = ResolveAssembly(partialName, HigherFrameworkReferenceAssemblyPaths, HigherFrameworkReferenceAssemblyPaths,
                                                             false /*checkDependencies*/);
                if (r.ResolvedFiles != null && r.ResolvedFiles.Count > 0)
                {
                    return(ReferenceAssemblyType.FrameworkAssembly);
                }
            }

            return(ReferenceAssemblyType.NonFrameworkAssembly);
        }
        private static ReferenceAssemblyType GetPathToReferenceAssembly(Assembly a, out string path,
            ICollection<BuildErrorEventArgs> errors, ICollection<BuildWarningEventArgs> warnings,
            bool checkDependencies, bool useFullName, out AssemblyResolutionResult result) {
            // 1. Find the assembly using RAR in the target framework.
            //    - If found, assembly is a framework assembly. Done
            // 2. Find the assembly using RAR in higher frameworks.
            //    - If found, assembly is a framework assembly only present in a higher version. Done.
            // 3. Find the assembly using RAR in the full profile framework.
            //    - If found, assembly is a framework assembly, but is only present in the full profile framework and not the current target profile. Done.
            // 4. Is useFullName true?
            //    - Yes: Use GAC and directory of loaded assembly as search paths.
            //    - No: Use directory of loaded assembly as search path.
            //    - Use RAR to find assembly in search paths. 
            //    - Check for out of range dependencies.
            // 5. If useFullName
            //    - Check if the short name exists in a higher framework, if so, it is a framework assembly.

            // Find the assembly in the target framework.
            string assemblyName;
            string partialName = a.GetName().Name;
            if (useFullName) {
                // Use the actual assembly name as specified in the config.
                assemblyName = CompilationSection.GetOriginalAssemblyName(a);
            }
            else {
                assemblyName = partialName;
            }
            result = ResolveAssembly(assemblyName, TargetFrameworkReferenceAssemblyPaths, 
                TargetFrameworkReferenceAssemblyPaths, false /*checkDependencies*/);
            if (result.ResolvedFiles != null && result.ResolvedFiles.Count > 0) {
                path = result.ResolvedFiles.FirstOrDefault();
                return ReferenceAssemblyType.FrameworkAssembly;
            }

            // At this point, the assembly was not found in the target framework.
            // Try finding it in the latest framework.
            result = ResolveAssembly(assemblyName, HigherFrameworkReferenceAssemblyPaths, HigherFrameworkReferenceAssemblyPaths, 
                false /*checkDependencies*/);
            if (result.ResolvedFiles != null && result.ResolvedFiles.Count > 0) {
                path = result.ResolvedFiles.FirstOrDefault();
                // Assembly was found in a target framework of a later version.
                return ReferenceAssemblyType.FrameworkAssemblyOnlyPresentInHigherVersion;
            }

            // Try to find the assembly in the full profile, in case the user 
            // is using an assembly that is not in the target profile framework. 
            // For example, System.Web is not present in the Client profile, but is present in the full profile.
            if (NeedToCheckFullProfile) {
                result = ResolveAssembly(assemblyName, FullProfileReferenceAssemblyPaths, FullProfileReferenceAssemblyPaths,
                    false /*checkDependencies*/);
                if (result.ResolvedFiles != null && result.ResolvedFiles.Count > 0) {
                    // Assembly was found in the full profile, but not in the target profile.
                    path = result.ResolvedFiles.FirstOrDefault();
                    // Report warning/error message.
                    string profile = "";
                    if (!string.IsNullOrEmpty(MultiTargetingUtil.TargetFrameworkName.Profile)) {
                        profile = " '" + MultiTargetingUtil.TargetFrameworkName.Profile + "'";
                    }
                    ReportWarningOrError(SR.GetString(SR.Assembly_not_found_in_profile, assemblyName, profile));
                    // Return as OnlyPresentInHigherVersion so that it will not be used as a reference assembly.
                    return ReferenceAssemblyType.FrameworkAssemblyOnlyPresentInHigherVersion;
                }
            }

            // Assembly is not found in the framework.
            // Check whether it has any references to assemblies of a higher version.
            List<string> searchPaths = new List<string>();
            searchPaths.AddRange(TargetFrameworkReferenceAssemblyPaths);
            searchPaths.Add(Path.GetDirectoryName(a.Location));
            // If we are using full names, include the GAC so that we can retrieve the actual
            // specified version of an OOB assembly even if it is unified/redirected to a later version.
            // For example, System.Web.Extensions 1.0.61025 is available from the GAC, but the actual
            // loaded assembly is 4.0 due to unification.
            if (useFullName) {
                searchPaths.Add("{GAC}");
            }

            // When checking dependencies of a custom assembly, use the full
            // name of the assembly as it might have a strong name or
            // be in the GAC.
            if (!useFullName) {
                assemblyName = a.GetName().FullName;
            }
            result = ResolveAssembly(assemblyName, searchPaths, TargetFrameworkReferenceAssemblyPaths, checkDependencies);
            // Use the actual resolved path, in case the loaded assembly is different from the specified assembly 
            // due to unification or binding redirect.
            path = result.ResolvedFiles.FirstOrDefault();

            if (string.IsNullOrEmpty(path)) {
                // In some cases, we might not be able to resolve the path to the assembly successfully, for example when
                // the config specifies the full name as System.Web 4.0.10101.0. Assembly.Load returns the 4.0.0.0 version,
                // but we can't find any actual assembly with such a full name.
                path = System.Web.UI.Util.GetAssemblyCodeBase(a);
            }

            // If we are using full names, do another check using the partial name to see if the assembly is part of 
            // a higher framework.
            // If so, then this is an OOB assembly that later got rolled into the framework, so we consider the assembly
            // as a framework assembly.
            if (useFullName) {
                AssemblyResolutionResult r = ResolveAssembly(partialName, HigherFrameworkReferenceAssemblyPaths, HigherFrameworkReferenceAssemblyPaths,
                    false /*checkDependencies*/);
                if (r.ResolvedFiles != null && r.ResolvedFiles.Count > 0) {
                    return ReferenceAssemblyType.FrameworkAssembly;
                }
            }

            return ReferenceAssemblyType.NonFrameworkAssembly;
        }
 private static void StoreResults(Assembly a, string path, AssemblyResolutionResult result, ReferenceAssemblyType assemblyType) {
     lock (s_lock) {
         if (!AssemblyLocations.ContainsKey(a)) {
             AssemblyLocations.Add(a, path);
             AssemblyResolutionResults.Add(a, result);
             ReferenceAssemblyTypes.Add(a, assemblyType);
         }
     }
 }
        /// <summary>
        /// Resolve a single assembly using the provided search paths and setting the targetframework directories.
        /// </summary>
        private static AssemblyResolutionResult ResolveAssembly(string assemblyName, IList<string> searchPaths, IList<string> targetFrameworkDirectories, bool checkDependencies) {
            ResolveAssemblyReference rar = new ResolveAssemblyReference();
            MockEngine engine = new MockEngine();
            rar.BuildEngine = engine;
            if (searchPaths != null) {
                rar.SearchPaths = searchPaths.ToArray();
            }
            if (targetFrameworkDirectories != null) {
                rar.TargetFrameworkDirectories = targetFrameworkDirectories.ToArray();
            }
            rar.Assemblies = new ITaskItem[] {
                new TaskItem(assemblyName),
            };
            rar.Silent = true;
            rar.Execute();

            AssemblyResolutionResult result = new AssemblyResolutionResult();

            List<string> resolvedFiles = new List<string>();
            foreach (ITaskItem item in rar.ResolvedFiles) {
                resolvedFiles.Add(item.ItemSpec);
            }
            if (checkDependencies) {
                CheckOutOfRangeDependencies(assemblyName);
            }
            result.ResolvedFiles = resolvedFiles.ToArray();
            result.Warnings = engine.Warnings;
            result.Errors = engine.Errors;
            return result;
        }
 private static ReferenceAssemblyType GetPathToReferenceAssembly(Assembly a, out string path, ICollection<BuildErrorEventArgs> errors, ICollection<BuildWarningEventArgs> warnings, bool checkDependencies, bool useFullName, out AssemblyResolutionResult result)
 {
     string originalAssemblyName;
     string name = a.GetName().Name;
     if (useFullName)
     {
         originalAssemblyName = CompilationSection.GetOriginalAssemblyName(a);
     }
     else
     {
         originalAssemblyName = name;
     }
     result = ResolveAssembly(originalAssemblyName, TargetFrameworkReferenceAssemblyPaths, TargetFrameworkReferenceAssemblyPaths, false);
     if ((result.ResolvedFiles != null) && (result.ResolvedFiles.Count > 0))
     {
         path = result.ResolvedFiles.FirstOrDefault<string>();
         FixMscorlibPath(a, ref path);
         return ReferenceAssemblyType.FrameworkAssembly;
     }
     result = ResolveAssembly(originalAssemblyName, HigherFrameworkReferenceAssemblyPaths, HigherFrameworkReferenceAssemblyPaths, false);
     if ((result.ResolvedFiles != null) && (result.ResolvedFiles.Count > 0))
     {
         path = result.ResolvedFiles.FirstOrDefault<string>();
         return ReferenceAssemblyType.FrameworkAssemblyOnlyPresentInHigherVersion;
     }
     if (NeedToCheckFullProfile)
     {
         result = ResolveAssembly(originalAssemblyName, FullProfileReferenceAssemblyPaths, FullProfileReferenceAssemblyPaths, false);
         if ((result.ResolvedFiles != null) && (result.ResolvedFiles.Count > 0))
         {
             path = result.ResolvedFiles.FirstOrDefault<string>();
             string str3 = "";
             if (!string.IsNullOrEmpty(MultiTargetingUtil.TargetFrameworkName.Profile))
             {
                 str3 = " '" + MultiTargetingUtil.TargetFrameworkName.Profile + "'";
             }
             ReportWarningOrError(System.Web.SR.GetString("Assembly_not_found_in_profile", new object[] { originalAssemblyName, str3 }));
             return ReferenceAssemblyType.FrameworkAssemblyOnlyPresentInHigherVersion;
         }
     }
     List<string> searchPaths = new List<string>();
     searchPaths.AddRange(TargetFrameworkReferenceAssemblyPaths);
     searchPaths.Add(Path.GetDirectoryName(a.Location));
     if (useFullName)
     {
         searchPaths.Add("{GAC}");
     }
     if (!useFullName)
     {
         originalAssemblyName = a.GetName().FullName;
     }
     result = ResolveAssembly(originalAssemblyName, searchPaths, TargetFrameworkReferenceAssemblyPaths, checkDependencies);
     path = result.ResolvedFiles.FirstOrDefault<string>();
     if (string.IsNullOrEmpty(path))
     {
         path = Util.GetAssemblyCodeBase(a);
     }
     if (useFullName)
     {
         AssemblyResolutionResult result2 = ResolveAssembly(name, HigherFrameworkReferenceAssemblyPaths, HigherFrameworkReferenceAssemblyPaths, false);
         if ((result2.ResolvedFiles != null) && (result2.ResolvedFiles.Count > 0))
         {
             return ReferenceAssemblyType.FrameworkAssembly;
         }
     }
     return ReferenceAssemblyType.NonFrameworkAssembly;
 }