tools/VsIdeHostAdapter/TargetAddIn/AgentSide/VsIdeHostAdapter.cs
/// <summary>
/// If run under debugger, attach to the host session of test runner IDE.
/// </summary>
If run under debugger, attach to the host session of test runner IDE.
If run under debugger attach to the host session of test runner IDE .
private void InitHostSession()
        {
            Debug.Assert(m_hostSession == null, "HA.InitHostSession: m_hostSession should be null!");
            Debug.Assert(m_runConfig != null);
            if (m_runConfig.IsExecutedUnderDebugger)
            {
                string sessionId = null;
                VsIdeHostRunConfigData hostData = m_runConfig.HostData[Name] as VsIdeHostRunConfigData;
                if (hostData != null)
                {
                    sessionId = hostData.SessionId;
                }
                else
                {
                    try
                    {
                        int ppid = ProcessUtil.GetParentProcessId(System.Diagnostics.Process.GetCurrentProcess().Id);
                        sessionId = VsIdeHostSession.Prefix + ppid.ToString(CultureInfo.InvariantCulture);
                    }
                    catch (COMException ex)
                    {
                        m_runContext.ResultSink.AddResult(new TestRunTextResultMessage(Environment.MachineName, m_runContext.RunConfig.TestRun.Id,
                            "Warning: " + ex.GetType() + ": " + ex.Message, TestMessageKind.TextMessage));
                    }
                }
                Debug.Assert(m_hostSession == null, "HA.InitHostSession: m_hostSession should be null!");
                if (!string.IsNullOrEmpty(sessionId))
                {
                    string uri = string.Format(
                        CultureInfo.InvariantCulture, "ipc://{0}/{1}",
                        sessionId,
                        VsIdeHostSession.RemoteObjectName);
                    m_hostSession = (IVsIdeHostDebugger)Activator.GetObject(typeof(IVsIdeHostDebugger), uri);
                }
                else
                {
                    m_runContext.ResultSink.AddResult(new TestRunTextResultMessage(Environment.MachineName, m_runContext.RunConfig.TestRun.Id,
                        "Warning: failed to get session id for debugger VS IDE, debugging child VS will be disabled.", TestMessageKind.TextMessage));
                }
            }
        }
private void InitHostSession ( ) { Debug . Assert ( m_hostSession == null , " " ) ; Debug . Assert ( m_runConfig != null ) ; if ( m_runConfig . IsExecutedUnderDebugger ) { string sessionId = null ; VsIdeHostRunConfigData hostData = m_runConfig . HostData [ Name ] as VsIdeHostRunConfigData ; if ( hostData != null ) { sessionId = hostData . SessionId ; } else { try { int ppid = ProcessUtil . GetParentProcessId ( System . Diagnostics . Process . GetCurrentProcess ( ) . Id ) ; sessionId = VsIdeHostSession . Prefix + ppid . ToString ( CultureInfo . InvariantCulture ) ; } catch ( COMException ex ) { m_runContext . ResultSink . AddResult ( new TestRunTextResultMessage ( Environment . MachineName , m_runContext . RunConfig . TestRun . Id , " " + ex . GetType ( ) + " " + ex . Message , TestMessageKind . TextMessage ) ) ; } } Debug . Assert ( m_hostSession == null , " " ) ; if ( ! string . IsNullOrEmpty ( sessionId ) ) { string uri = string . Format ( CultureInfo . InvariantCulture , " " , sessionId , VsIdeHostSession . RemoteObjectName ) ; m_hostSession = ( IVsIdeHostDebugger ) Activator . GetObject ( typeof ( IVsIdeHostDebugger ) , uri ) ; } else { m_runContext . ResultSink . AddResult ( new TestRunTextResultMessage ( Environment . MachineName , m_runContext . RunConfig . TestRun . Id , " " , TestMessageKind . TextMessage ) ) ; } } }
If run under debugger, attach to the host session of test runner IDE.
If run under debugger attach to the host session of test runner IDE .
// First, check if there is Host Data in Run Config. // There is no host data when using HostType attribute (not using Run Config). // Try connecting by parent process id. // This is not very reliable because Windows does not really track parent-child process relationship. // Now, get the IDE runner session, the Uri is ipc://ServerPortName/AppName.