public virtual bool SetupChannel()

in src/Microsoft.TestPlatform.CrossPlatEngine/Client/ProxyOperationManager.cs [169:293]


    public virtual bool SetupChannel(IEnumerable<string> sources, string runSettings)
    {
        CancellationTokenSource.Token.ThrowTestPlatformExceptionIfCancellationRequested();

        if (_initialized)
        {
            return true;
        }

        var connTimeout = EnvironmentHelper.GetConnectionTimeout();

        _testHostProcessStdError = string.Empty;
        TestHostConnectionInfo testHostConnectionInfo = TestHostManager.GetTestHostConnectionInfo();

        var portNumber = 0;
        if (testHostConnectionInfo.Role == ConnectionRole.Client)
        {
            portNumber = RequestSender.InitializeCommunication();
            testHostConnectionInfo.Endpoint += portNumber;
        }

        var processId = _processHelper.GetCurrentProcessId();
        var connectionInfo = new TestRunnerConnectionInfo()
        {
            Port = portNumber,
            ConnectionInfo = testHostConnectionInfo,
            RunnerProcessId = processId,
            LogFile = GetTimestampedLogFile(EqtTrace.LogFile),
            TraceLevel = (int)EqtTrace.TraceLevel
        };

        // Subscribe to test host events.
        TestHostManager.HostLaunched += TestHostManagerHostLaunched;
        TestHostManager.HostExited += TestHostManagerHostExited;

        // Get environment variables from run settings.
        var envVars = InferRunSettingsHelper.GetEnvironmentVariables(runSettings);

        // Get the test process start info.
        var testHostStartInfo = UpdateTestProcessStartInfo(
            TestHostManager.GetTestHostProcessStartInfo(
                sources,
                envVars,
                connectionInfo));
        try
        {
            // Launch the test host.
            _testHostLaunched = TestHostManager.LaunchTestHostAsync(
                testHostStartInfo,
                CancellationTokenSource.Token).Result;

            if (_testHostLaunched && testHostConnectionInfo.Role == ConnectionRole.Host)
            {
                // If test runtime is service host, try to poll for connection as client.
                RequestSender.InitializeCommunication();
            }
        }
        catch (Exception ex)
        {
            EqtTrace.Error("ProxyOperationManager: Failed to launch testhost :{0}", ex);

            CancellationTokenSource.Token.ThrowTestPlatformExceptionIfCancellationRequested();
            throw new TestPlatformException(string.Format(
                CultureInfo.CurrentUICulture,
                CrossPlatEngineResources.FailedToLaunchTestHost,
                ex.ToString()));
        }

        // Warn the user that execution will wait for debugger attach.
        var hostDebugEnabled = Environment.GetEnvironmentVariable("VSTEST_HOST_DEBUG");
        var nativeHostDebugEnabled = Environment.GetEnvironmentVariable("VSTEST_HOST_NATIVE_DEBUG");

        if ((!string.IsNullOrEmpty(hostDebugEnabled)
             && hostDebugEnabled.Equals("1", StringComparison.Ordinal))
            || (new PlatformEnvironment().OperatingSystem.Equals(PlatformOperatingSystem.Windows)
                && !string.IsNullOrEmpty(nativeHostDebugEnabled)
                && nativeHostDebugEnabled.Equals("1", StringComparison.Ordinal)))
        {
            ConsoleOutput.Instance.WriteLine(
                CrossPlatEngineResources.HostDebuggerWarning,
                OutputLevel.Warning);

            ConsoleOutput.Instance.WriteLine(
                string.Format(
                    "Process Id: {0}, Name: {1}",
                    _testHostProcessId,
                    _processHelper.GetProcessName(_testHostProcessId)),
                OutputLevel.Information);

            // Increase connection timeout when debugging is enabled.
            connTimeout *= 5;
        }

        // If test host does not launch then throw exception, otherwise wait for connection.
        if (!_testHostLaunched
            || !RequestSender.WaitForRequestHandlerConnection(
                connTimeout * 1000,
                CancellationTokenSource.Token))
        {
            EqtTrace.Verbose($"Test host failed to start Test host launched:{_testHostLaunched} test host exited: {_testHostExited.IsSet}");
            // Throw a test platform exception with the appropriate message if user requested cancellation.
            CancellationTokenSource.Token.ThrowTestPlatformExceptionIfCancellationRequested();

            // Throw a test platform exception along with the error messages from the test if the test host exited unexpectedly
            // before communication was established.
            ThrowOnTestHostExited(_testHostExited.IsSet);

            // Throw a test platform exception stating the connection to test could not be established even after waiting
            // for the configure timeout period.
            ThrowExceptionOnConnectionFailure(connTimeout);
        }

        // Handling special case for dotnet core projects with older test hosts.
        // Older test hosts are not aware of protocol version check, hence we should not be
        // sending VersionCheck message to these test hosts.
        CompatIssueWithVersionCheckAndRunsettings();
        if (_versionCheckRequired)
        {
            RequestSender.CheckVersionWithTestHost();
        }

        _initialized = true;

        return true;
    }