You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Visual Studio中实现类似VS Code launch.json的自定义调试启动?

Great question! I've struggled with this exact scenario before when trying to avoid the overhead of VS Docker Tools while still getting a smooth debug workflow. Here's how you can replicate VS Code's launch.json flexibility in Visual Studio:

Core Replacement for launch.json in Visual Studio

Visual Studio uses Launch Profiles (stored in Properties/launchSettings.json for .NET projects) as its equivalent to VS Code's launch.json. This file lets you define custom startup behaviors, commands, and environment variables for debugging.


Scenario 1: Custom Docker-Compose Launch + Attach to Container

If you want to run docker-compose without VS Docker Tools and automatically attach the debugger afterward, follow these steps:

1. Create a Custom Launch Profile

You can either edit launchSettings.json directly or use the UI:

  • Right-click your project → Properties → Debug → Add → Select Custom as the profile type.
  • Or paste this into launchSettings.json:
    "Custom Docker Debug": {
      "commandName": "Executable",
      "executablePath": "powershell.exe",
      "commandLineArgs": "-ExecutionPolicy Bypass -File \"$(ProjectDir)Start-DockerDebug.ps1\"",
      "workingDirectory": "$(ProjectDir)",
      "launchBrowser": false,
      "environmentVariables": {}
    }
    

2. Write a PowerShell Script to Orchestrate the Workflow

Create a Start-DockerDebug.ps1 file in your project root with this logic (adjust container names and timings to match your setup):

# Step 1: Build and start Docker containers
Write-Host "Starting Docker Compose services..."
docker-compose up -d --build

# Step 2: Wait for the target container to be fully running
$targetContainer = "your-app-container-name"
while (-not (docker inspect -f '{{.State.Running}}' $targetContainer 2>$null)) {
    Start-Sleep -Seconds 1
    Write-Host "Waiting for container $targetContainer to start..."
}

# Step 3: Get the container's host PID and attach Visual Studio debugger
$containerPid = docker inspect -f '{{.State.Pid}}' $targetContainer
Write-Host "Attaching debugger to container PID: $containerPid"
devenv /command "Debug.AttachToProcess /processid:$containerPid"

3. Manual Attach Fallback

If you prefer not to use a script:

  • Run docker-compose up -d --build via your terminal or Visual Studio's Package Manager Console
  • Go to Debug → Attach to Process
  • In the "Connection Type" dropdown, select Docker
  • Pick your running container from the list and click Attach

Scenario 2: General Custom Debug Workflows

For any scenario where you need to run a sequence of commands (build, setup dependencies, launch app, etc.), use these tools:

1. Pre/Post Launch Commands in Launch Profiles

Edit launchSettings.json to add commands that run before or after debugging starts:

"Custom Build & Debug": {
    "commandName": "Project",
    "preLaunchCommand": "dotnet build -c Debug && echo Dependencies built!",
    "postLaunchCommand": "echo Debug session initiated!",
    "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
    }
}

2. MSBuild Custom Targets

For complex build/setup logic, add custom targets to your project file (.csproj/.vbproj):

<Target Name="PreDebugSetup" BeforeTargets="Debug">
    <!-- Start dependent services -->
    <Exec Command="docker-compose up -d redis" WorkingDirectory="$(ProjectDir)" />
    <!-- Wait for services to be ready -->
    <Exec Command="powershell -Command &quot;Start-Sleep -Seconds 3&quot;" />
</Target>

Visual Studio will automatically run this target before starting the debugger.

3. External Tools (For Non-.NET Projects)

For C++ or other project types:

  • Go to Tools → External Tools
  • Add a new tool with your command (e.g., docker-compose up -d)
  • Assign a keyboard shortcut, then run it manually before starting debugging

Key Notes
  • Ensure your containerized app has debugging symbols (e.g., copy .pdb files in your Dockerfile for .NET)
  • For .NET apps, set ASPNETCORE_ENVIRONMENT=Development in your container to enable debug mode
  • Verify docker-compose is in your system PATH, or use the full path in your commands

内容的提问来源于stack exchange,提问作者Swimburger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:55:11