如何在“开始调试”时运行自定义代码在Visual Studio中并附加到调试器 [英] How to run custom code when "Start Debugging" in Visual Studio and attach to debugger

查看:132
本文介绍了如何在“开始调试”时运行自定义代码在Visual Studio中并附加到调试器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VS Code似乎有一个名为Launch.json的文件来自定义运行应用程序进行调试时会发生什么。
我们如何为Visual Studio进行此操作?
我的目标是在没有VS Docker工具的情况下运行docker-compose,然后在容器启动后告诉VS附加到容器。

VS Code seems to have a file called Launch.json to customize what happens when you run your app for debugging. How do we go about that for Visual Studio? My goals is to run docker-compose without the VS Docker Tools, and then after the containers are up, telling VS to attach to the container.

My问题不是特定于那种情况,它也可能是另一种自定义的Run with Debugger方案。
理想情况下,我可以运行一些命令来构建项目,运行项目,然后告诉VS附加到它并将该逻辑与VS关联。

My question is not specific to that case though, it could just as well be another custom way of Run with debugger scenario. Ideally I could just run some commands to build the project, run the project, and then tell VS to attach to it and associate that logic with VS.

推荐答案

根据Vlad和Jack的答案,我提出了以下解决方案。
要在我按下运行按钮时运行自己的代码,我会设置一个带有自定义启动设置的空白命令行项目。

Based on the answers of Vlad and Jack, I came up with the following solution. To have my own code run when I press the Run button, I set up a blank Command Line project with custom launch settings.

{
  "profiles": {
    "Build": {
      "commandName": "Executable",
      "executablePath": "powershell",
      "commandLineArgs": ".\\DebugRun.ps1",
      "workingDirectory": "."
    }
  }
}

每当我按下Run,它将使用PowerShell运行 DebugRun.ps1 脚本。
这是我放在 DebugRun.ps1 中的内容。

Whenever I press Run, it will run the DebugRun.ps1 script using PowerShell. Here's what I put in the DebugRun.ps1.

docker-compose -f "./docker-compose.debug.yml" --no-ansi up -d --force-recreate --build

Start-Sleep -Seconds 5

$appId = ((docker ps --filter "ancestor=employeemapapp:debug")[1] -split " ")[0]
$apiId = ((docker ps --filter "ancestor=employeemapapi:debug")[1] -split " ")[0]

$appIp = (docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $appId)
$apiIp = (docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $apiId)

docker exec -d $appId C:\\remote_debugger\\x64\\msvsmon.exe /noauth /anyuser /silent /nostatus /noclrwarn /nosecuritywarn /nofirewallwarn /nowowwarn /timeout:214748364
docker exec -d $apiId C:\\remote_debugger\\x64\\msvsmon.exe /noauth /anyuser /silent /nostatus /noclrwarn /nosecuritywarn /nofirewallwarn /nowowwarn /timeout:214748364

$appTarget = $appId + ":4022"
$apiTarget = $apiId + ":4022"

#Parameters
## 1: Solution Name is used to run the code only to the VS instance that has the Solution open
## 2: Transportation method for remote debugger
## 3: Target is the hostname/IP/... to the target where remote debugging is running
## 4: The process name you want to attach tos
./RemoteDebugAttach.exe "EmployeeMap.sln" "Remote (no authentication)" $appTarget "dotnet.exe"
./RemoteDebugAttach.exe "EmployeeMap.sln" "Remote (no authentication)" $apiTarget "dotnet.exe"

Write-Host "Api:" $apiIp
Write-Host "App:" $appIp
$apiUrl = "http://$apiIp/api/employees"
$appUrl = "http://$appIp"

start $apiUrl
start $appUrl

Read-Host "Press any key to quit"

./RemoteDebugDetach.exe EmployeeMap.sln "dotnet.exe"

docker exec -d $appId C:\\remote_debugger\\x64\\utils\\KillProcess.exe msvsmon.exe
docker exec -d $apiId C:\\remote_debugger\\x64\\utils\\KillProcess.exe msvsmon.exe

docker-compose -f "./docker-compose.debug.yml" --no-ansi down 

此脚本执行以下操作:


  • 通过docker-compose打开我的docker容器

  • 抓住容器ID,ip等等。

  • 启动容器内的远程调试器实例

  • 附加到 dotnet.exe 使用自定义可执行文件(RemoteDebugAttach)进行容器内部处理

  • 打开浏览器以浏览从容器提供的网站

  • 等待任何按键降低基础设施

  • 使用自定义可执行文件从进程中分离

  • 在docker上杀死远程调试器容器

  • 关闭容器

  • Bring up my docker containers via docker-compose
  • Grab the container id's, ip's, etc
  • Start the remote debugger instance inside of the container
  • Attach to the dotnet.exe processes inside of the container using a custom executable (RemoteDebugAttach)
  • Open the browsers to browse to the websites served from the containers
  • Wait for any keypress to bring down infrastructure
  • Detach from processes using custom executable
  • Kill remote debugger on docker container
  • Bring down containers

自定义功能es来自一个单独的解决方案,我刚刚构建了一些命令行应用程序。
我使用了这个代码 source 以使所有VS实例在计算机上运行。
并将其与此代码相结合以附加:

The custom executables come from a separate solution where I just built some Command Line Applications. I used the code from this source to get all VS instances running on the machine. And combined it with this code to attach:

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        string solutionName = Ask(args, 0, "Solution name?");
        string transportName = Ask(args, 1, "Transport name?");
        string target = Ask(args, 2, "Target machine?");
        string processName = Ask(args, 3, "Process Name?");


        var instances = Msdev.GetIDEInstances(true);
        var dte = (DTE2)instances.Find(d => d.Solution.FullName.EndsWith(solutionName, StringComparison.InvariantCultureIgnoreCase));
        var debugger = dte.Debugger as Debugger2;
        var transports = debugger.Transports;
        Transport transport = null;
        foreach(Transport loopTransport in transports)
        {
            if(loopTransport.Name.Equals(transportName, StringComparison.InvariantCultureIgnoreCase)) // "Remote (no authentication)")
            {
                transport = loopTransport;
                break;
            }
        }

        Processes processes = debugger.GetProcesses(transport, target); // "172.24.50.15:4022");
        foreach(Process process in processes)
        {
            if(process.Name.EndsWith(processName, StringComparison.InvariantCultureIgnoreCase))
            {
                process.Attach();
            }
        }
    }

    static string Ask(string[] args, int index, string question)
    {
        if(args.Length <= index)
        {
            Console.WriteLine(question);
            return Console.ReadLine();
        }

        return args[index];
    }
}

以下内容分离:

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        string solutionName = Ask(args, 0, "Solution name");
        string processName = Ask(args, 1, "Process Name?");

        var instances = Msdev.GetIDEInstances(true);

        var dte = (DTE2)instances.Find(d => d.Solution.FullName.EndsWith(solutionName, StringComparison.InvariantCultureIgnoreCase));
        var debugger = dte.Debugger as Debugger2;

        Processes processes = debugger.DebuggedProcesses;
        foreach (Process2 process in processes)
        {
            if (process.Name.EndsWith(processName, StringComparison.InvariantCultureIgnoreCase))
            {
                process.Detach(false);
            }
        }
    }

    static string Ask(string[] args, int index, string question)
    {
        if (args.Length <= index)
        {
            Console.WriteLine(question);
            return Console.ReadLine();
        }

        return args[index];
    }
}

我本来希望在PowerShell中拥有此代码,因为复制到其他项目+编辑会更容易。虽然在PowerShell中,我根本无法获得正确的代码来执行,即使使用反射将某些代码应用于COM对象。

I would have preferred to have this code in PowerShell because it would be easier to copy to other project + edit. Though in PowerShell, I was simply not able to get the right code to execute, even when using reflection to apply certain code the COM objects.

我希望它可以帮助一些人谁想拥有自己的自定义流程构建到VS.
谢谢弗拉德和杰克。

I hope it helps some people who want to have their own custom flow build into VS. Thank you Vlad and Jack.

这篇关于如何在“开始调试”时运行自定义代码在Visual Studio中并附加到调试器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆