Example #1
0
        public bool AttachDebugger(int processId)
        {
            try
            {
                var stopWatch = Stopwatch.StartNew();

                var proxy = DebuggerAttacherServiceConfiguration.CreateProxy(_debuggingNamedPipeId, _timeout);
                using (var client = new DebuggerAttacherServiceProxyWrapper(proxy))
                {
                    client.Service.AttachDebugger(processId);
                    stopWatch.Stop();
                    _logger.DebugInfo($"Debugger attached to process {processId}, took {stopWatch.ElapsedMilliseconds} ms");
                    return(true);
                }
            }
            catch (FaultException <DebuggerAttacherServiceFault> serviceFault)
            {
                var errorMessage = serviceFault.Detail.Message;
                if (string.IsNullOrWhiteSpace(errorMessage))
                {
                    errorMessage = $"Could not attach debugger to process {processId}, no error message available";
                }

                errorMessage += $"{Environment.NewLine}There might be more information on the problem in Visual Studio's ActivityLog.xml (see e.g. https://blogs.msdn.microsoft.com/visualstudio/2010/02/24/troubleshooting-extensions-with-the-activity-log/)";

                _logger.LogError(errorMessage);
            }
            catch (Exception e)
            {
                _logger.LogError($"Could not attach debugger to process {processId}:{Environment.NewLine}{e}");
            }
            return(false);
        }
        public bool AttachDebugger(int processId, DebuggerEngine debuggerEngine)
        {
            try
            {
                var stopWatch = Stopwatch.StartNew();

                var proxy = DebuggerAttacherServiceConfiguration.CreateProxy(_debuggingNamedPipeId, _timeout);
                using (var client = new DebuggerAttacherServiceProxyWrapper(proxy))
                {
                    client.Service.AttachDebugger(processId, debuggerEngine);
                    stopWatch.Stop();
                    _logger.DebugInfo(String.Format(Resources.DebuggerAttachTime, processId, stopWatch.ElapsedMilliseconds));
                    return(true);
                }
            }
            catch (FaultException <DebuggerAttacherServiceFault> serviceFault)
            {
                var errorMessage = serviceFault.Detail.Message;
                if (string.IsNullOrWhiteSpace(errorMessage))
                {
                    errorMessage = String.Format(Resources.DebuggerAttachMessage, processId);
                }

                errorMessage += $"{Environment.NewLine}{Resources.MoreInformationMessage}";

                _logger.LogError(errorMessage);
            }
            catch (Exception e)
            {
                _logger.LogError(String.Format(Resources.CouldNotAttachMessage, processId, Environment.NewLine, e));
            }
            return(false);
        }
        private void DoTest(string expectedErrorMessagePart)
        {
            string pipeId            = Guid.NewGuid().ToString();
            int    debuggeeProcessId = 2017;

            // ReSharper disable once UnusedVariable
            var host = new DebuggerAttacherServiceHost(pipeId, MockDebuggerAttacher.Object, MockLogger.Object);

            try
            {
                host.Open();

                var proxy = DebuggerAttacherServiceConfiguration.CreateProxy(pipeId, WaitingTime);
                using (var client = new DebuggerAttacherServiceProxyWrapper(proxy))
                {
                    client.Should().NotBeNull();
                    client.Service.Should().NotBeNull();

                    Action attaching = () => client.Service.AttachDebugger(debuggeeProcessId);
                    if (expectedErrorMessagePart == null)
                    {
                        attaching.ShouldNotThrow();
                    }
                    else
                    {
                        attaching.ShouldThrow <FaultException <DebuggerAttacherServiceFault> >().Where(
                            (FaultException <DebuggerAttacherServiceFault> ex) => ex.Detail.Message.Contains(expectedErrorMessagePart));
                    }
                }

                host.Close();
            }
            catch (CommunicationException)
            {
                host.Abort();
                throw;
            }
        }