根据 Vlad 和 Jack 的回答,我想出了以下解决方案。
为了在我按下运行按钮时运行我自己的代码,我设置了一个带有自定义启动设置的空白命令行项目。
{
"profiles": {
"Build": {
"commandName": "Executable",
"executablePath": "powershell",
"commandLineArgs": ".\\DebugRun.ps1",
"workingDirectory": "."
}
}
}
每当我按下运行时,它都会使用 PowerShell 运行 DebugRun.ps1 脚本。
这是我在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 等
- 在容器内启动远程调试器实例
- 使用自定义可执行文件 (RemoteDebugAttach) 附加到容器内的
dotnet.exe 进程
- 打开浏览器以浏览从容器提供的网站
- 等待任何按键来关闭基础架构
- 使用自定义可执行文件从进程中分离
- 杀死 docker 容器上的远程调试器
- 拆下容器
自定义可执行文件来自一个单独的解决方案,我刚刚在其中构建了一些命令行应用程序。
我使用source 中的代码让所有 VS 实例在机器上运行。
并将其与此代码结合起来附加:
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 对象时也是如此。
我希望它可以帮助一些想要在 VS 中构建自己的自定义流程的人。
谢谢弗拉德和杰克。