private void AddSearchDirectoriesSpecifiedInRunSettingsToAssemblyResolver(AssemblyResolver assemblyResolver, string baseDirectory)
        {
            // Check if user specified any adapter settings
            MSTestAdapterSettings adapterSettings = MSTestSettingsProvider.Settings;

            if (adapterSettings != null)
            {
                try
                {
                    var additionalSearchDirectories = adapterSettings.GetDirectoryListWithRecursiveProperty(baseDirectory);
                    if (additionalSearchDirectories?.Count > 0)
                    {
                        assemblyResolver.AddSearchDirectoriesFromRunSetting(additionalSearchDirectories);
                    }
                }
                catch (Exception exception)
                {
                    EqtTrace.Error(
                        "DesktopTestSourceHost.AddSearchDirectoriesSpecifiedInRunSettingsToAssemblyResolver(): Exception hit while trying to set assembly resolver for domain. Exception : {0} \n Message : {1}",
                        exception,
                        exception.Message);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Setup the isolation host.
        /// </summary>
        public void SetupHost()
        {
            List <string> resolutionPaths = this.GetResolutionPaths(this.sourceFileName, VSInstallationUtilities.IsCurrentProcessRunningInPortableMode());

            // Check if user specified any runsettings
            MSTestAdapterSettings adapterSettings = MSTestSettingsProvider.Settings;

            if (resolutionPaths != null && resolutionPaths.Count > 0)
            {
                if (EqtTrace.IsInfoEnabled)
                {
                    EqtTrace.Info("TestSourceHost: Creating assembly resolver with resolution paths {0}.", string.Join(",", resolutionPaths.ToArray()));
                }

                // Adding adapter folder to resolution paths
                if (!resolutionPaths.Contains(Path.GetDirectoryName(typeof(TestSourceHost).Assembly.Location)))
                {
                    resolutionPaths.Add(Path.GetDirectoryName(typeof(TestSourceHost).Assembly.Location));
                }

                // Adding extensions folder to resolution paths
                if (!resolutionPaths.Contains(Path.GetDirectoryName(typeof(AssemblyHelper).Assembly.Location)))
                {
                    resolutionPaths.Add(Path.GetDirectoryName(typeof(AssemblyHelper).Assembly.Location));
                }
            }

            // Honour DisableAppDomain setting if it is present in runsettings
            if (this.runSettings != null && MSTestAdapterSettings.IsAppDomainCreationDisabled(this.runSettings.SettingsXml))
            {
                if (adapterSettings != null)
                {
                    try
                    {
                        this.assemblyResolver = new AssemblyResolver(resolutionPaths);
                        this.assemblyResolver.AddSearchDirectoriesFromRunSetting(adapterSettings.GetDirectoryListWithRecursiveProperty(null));
                    }
                    catch (Exception exception)
                    {
                        if (EqtTrace.IsErrorEnabled)
                        {
                            EqtTrace.Error(exception);
                        }
                    }
                }
            }

            var appDomainSetup = new AppDomainSetup();

            // The below logic of preferential setting the appdomains appbase is needed because:
            // 1. We set this to the location of the test source if it is built for Full CLR  -> Ideally this needs to be done in all situations.
            // 2. We set this to the location where the current adapter is being picked up from for UWP and .Net Core scenarios -> This needs to be
            //    different especially for UWP because we use the desktop adapter(from %temp%\VisualStudioTestExplorerExtensions) itself for test discovery
            //    in IDE scenarios. If the app base is set to the test source location, discovery will not work because we drop the
            //    UWP platform service assembly at the test source location and since CLR starts looking for assemblies from the app base location,
            //    there would be a mismatch of platform service assemblies during discovery.
            var frameworkVersionString = this.GetTargetFrameworkVersionString(this.sourceFileName);

            if (frameworkVersionString.Contains(PlatformServices.Constants.DotNetFrameWorkStringPrefix))
            {
                appDomainSetup.ApplicationBase = Path.GetDirectoryName(this.sourceFileName)
                                                 ?? Path.GetDirectoryName(typeof(TestSourceHost).Assembly.Location);
            }
            else
            {
                appDomainSetup.ApplicationBase = Path.GetDirectoryName(typeof(TestSourceHost).Assembly.Location);
            }

            if (EqtTrace.IsInfoEnabled)
            {
                EqtTrace.Info("TestSourceHost: Creating app-domain for source {0} with application base path {1}.", this.sourceFileName, appDomainSetup.ApplicationBase);
            }

            AppDomainUtilities.SetAppDomainFrameworkVersionBasedOnTestSource(appDomainSetup, frameworkVersionString);

            var configFile = this.GetConfigFileForTestSource(this.sourceFileName);

            AppDomainUtilities.SetConfigurationFile(appDomainSetup, configFile);

            this.domain = this.appDomain.CreateDomain("TestSourceHost: Enumering assembly", null, appDomainSetup);

            // Load objectModel before creating assembly resolver otherwise in 3.5 process, we run into a recurive assembly resolution
            // which is trigged by AppContainerUtilities.AttachEventToResolveWinmd method.
            EqtTrace.SetupRemoteEqtTraceListeners(this.domain);

            // Add an assembly resolver...
            Type assemblyResolverType = typeof(AssemblyResolver);

            if (EqtTrace.IsInfoEnabled)
            {
                EqtTrace.Info("TestSourceHost: assemblyenumerator location: {0} , fullname: {1} ", assemblyResolverType.Assembly.Location, assemblyResolverType.FullName);
            }

            var resolver = AppDomainUtilities.CreateInstance(
                this.domain,
                assemblyResolverType,
                new object[] { resolutionPaths });

            if (EqtTrace.IsInfoEnabled)
            {
                EqtTrace.Info(
                    "TestSourceHost: resolver type: {0} , resolve type assembly: {1} ",
                    resolver.GetType().FullName,
                    resolver.GetType().Assembly.Location);
            }

            this.assemblyResolver = (AssemblyResolver)resolver;

            if (adapterSettings != null)
            {
                try
                {
                    var additionalSearchDirectories =
                        adapterSettings.GetDirectoryListWithRecursiveProperty(appDomainSetup.ApplicationBase);
                    if (additionalSearchDirectories?.Count > 0)
                    {
                        this.assemblyResolver.AddSearchDirectoriesFromRunSetting(
                            adapterSettings.GetDirectoryListWithRecursiveProperty(appDomainSetup.ApplicationBase));
                    }
                }
                catch (Exception exception)
                {
                    if (EqtTrace.IsErrorEnabled)
                    {
                        EqtTrace.Error(exception);
                    }
                }
            }
        }