Ejemplo n.º 1
0
        internal static string GetTargetFrameworkVersionString(string testSourcePath)
        {
            AppDomainSetup appDomainSetup = new AppDomainSetup();

            appDomainSetup.LoaderOptimization = LoaderOptimization.MultiDomainHost;

            AppDomainUtilities.SetConfigurationFile(appDomainSetup, new DeploymentUtility().GetConfigFile(testSourcePath));

            if (File.Exists(testSourcePath))
            {
                AppDomain appDomain = null;

                try
                {
                    appDomain = AppDomain.CreateDomain("Framework Version String Domain", null, appDomainSetup);

                    // Wire the eqttrace logs in this domain to the current domain.
                    EqtTrace.SetupRemoteEqtTraceListeners(appDomain);

                    // Add an assembly resolver to resolve ObjectModel or any Test Platform dependencies.
                    // Not moving to IMetaDataImport APIs because the time taken for this operation is <20 ms and
                    // IMetaDataImport needs COM registration which is not a guarantee in Dev15.
                    var assemblyResolverType = typeof(AssemblyResolver);

                    var resolutionPaths = new List <string> {
                        Path.GetDirectoryName(typeof(TestCase).Assembly.Location)
                    };
                    resolutionPaths.Add(Path.GetDirectoryName(testSourcePath));

                    AppDomainUtilities.CreateInstance(
                        appDomain,
                        assemblyResolverType,
                        new object[] { resolutionPaths });

                    var assemblyLoadWorker =
                        (AssemblyLoadWorker)AppDomainUtilities.CreateInstance(
                            appDomain,
                            typeof(AssemblyLoadWorker),
                            null);

                    return(assemblyLoadWorker.GetTargetFrameworkVersionStringFromPath(testSourcePath));
                }
                catch (Exception exception)
                {
                    if (EqtTrace.IsErrorEnabled)
                    {
                        EqtTrace.Error(exception);
                    }
                }
                finally
                {
                    if (appDomain != null)
                    {
                        AppDomain.Unload(appDomain);
                    }
                }
            }

            return(string.Empty);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns the dependent assemblies of the parameter assembly.
        /// </summary>
        /// <param name="assemblyPath"> Path to assembly to get dependencies for. </param>
        /// <param name="configFile"> Config file to use while trying to resolve dependencies. </param>
        /// <param name="warnings"> The warnings. </param>
        /// <returns> The <see cref="string[]"/>. </returns>
        internal virtual string[] GetFullPathToDependentAssemblies(string assemblyPath, string configFile, out IList <string> warnings)
        {
            Debug.Assert(!string.IsNullOrEmpty(assemblyPath), "assemblyPath");

            EqtTrace.InfoIf(EqtTrace.IsInfoEnabled, "AssemblyDependencyFinder.GetDependentAssemblies: start.");

            AppDomainSetup setupInfo = new AppDomainSetup();

            setupInfo.ApplicationBase = Path.GetDirectoryName(Path.GetFullPath(assemblyPath));

            Debug.Assert(string.IsNullOrEmpty(configFile) || File.Exists(configFile), "Config file is specified but does not exist: {0}", configFile);

            AppDomainUtilities.SetConfigurationFile(setupInfo, configFile);

            EqtTrace.InfoIf(EqtTrace.IsInfoEnabled, "AssemblyDependencyFinder.GetDependentAssemblies: Using config file: '{0}'.", setupInfo.ConfigurationFile);

            setupInfo.LoaderOptimization = LoaderOptimization.MultiDomainHost;

            AppDomain appDomain = null;

            try
            {
                appDomain = AppDomain.CreateDomain("Dependency finder domain", null, setupInfo);
                if (EqtTrace.IsInfoEnabled)
                {
                    EqtTrace.Info("AssemblyDependencyFinder.GetDependentAssemblies: Created AppDomain.");
                }

                var assemblyResolverType = typeof(AssemblyResolver);

                EqtTrace.SetupRemoteEqtTraceListeners(appDomain);

                // This has to be LoadFrom, otherwise we will have to use AssemblyResolver to find self.
                using (
                    AssemblyResolver resolver =
                        (AssemblyResolver)AppDomainUtilities.CreateInstance(
                            appDomain,
                            assemblyResolverType,
                            new object[] { this.GetResolutionPaths() }))
                {
                    // This has to be Load, otherwise Serialization of argument types will not work correctly.
                    AssemblyLoadWorker worker =
                        (AssemblyLoadWorker)AppDomainUtilities.CreateInstance(appDomain, typeof(AssemblyLoadWorker), null);

                    EqtTrace.InfoIf(EqtTrace.IsInfoEnabled, "AssemblyDependencyFinder.GetDependentAssemblies: loaded the worker.");

                    return(worker.GetFullPathToDependentAssemblies(assemblyPath, out warnings));
                }
            }
            finally
            {
                if (appDomain != null)
                {
                    EqtTrace.InfoIf(EqtTrace.IsInfoEnabled, "AssemblyDependencyFinder.GetDependentAssemblies: unloading AppDomain...");
                    AppDomain.Unload(appDomain);
                    EqtTrace.InfoIf(EqtTrace.IsInfoEnabled, "AssemblyDependencyFinder.GetDependentAssemblies: unloading AppDomain succeeded.");
                }
            }
        }