matthewrk
matthewrk

Reputation: 687

Debugging in vscode with multiple dotnet core projects under one solution

I have setup my project in vscode with a root "solution" containing a global.json defining the sub projects. These are currently my web app, and a class library.

That works fine, and I have setup the following launch.json under the root directory in an attempt to debug my web app:

{
"version": "0.2.0",
"configurations": [
    {
        "name": "WebApp",
        "type": "coreclr",
        "request": "launch",
        "preLaunchTask": "",
        "program": "${workspaceRoot}/WebApp/bin/Debug/netcoreapp1.0/WebApp.dll",
        "args": [],
        "cwd": "${workspaceRoot}/WebApp/",
        "stopAtEntry": false,
        "launchBrowser": {
            "enabled": true,
            "args": "${auto-detect-url}",
            "osx": {
                "command": "open"
            }
        }
    }
]

}

I have two issues with this.

  1. I had to remove the preLaunchTask build because it tries to build from the root dir. Not ideal but I can work around that by building manually first.

  2. When the debugger launches, it fails to find the source code because its looking in the root dir, rather than the sub project. This one is a show stopper because I can't use breakpoints at all without the source loaded.

Is there a way around these issues?

Update: 9th August 2016

I took the issue to Omnisharp directly and got a bit further, being able to debug an app and a separate library under one solution. Haven't quite hit the jackpot on multiple executable projects under one solution yet though. Unfortunately I'm not pursuing that project any longer but hopefully this can help someone towards a total solution.

Link to discussion and code samples: https://github.com/OmniSharp/omnisharp-vscode/issues/460#issuecomment-228392486

Upvotes: 11

Views: 7049

Answers (3)

Pukka
Pukka

Reputation: 140

I had the same problem, and I have the following structure for my solution:

global.json
|src
--|TestApp
----Program.cs
----project.json
--|ExtLib
----ExtLibClass.cs
----project.json

in the task.json file in the .vscode folder you have to set the options value like this:

"command": "dotnet",
"isShellCommand": true,
"options": {
    "cwd": "${workspaceRoot}/src/TestApp"
},

and in the launch.json file in the .vscode folder you have to change the program property like this:

"configurations": [
    {
        "name": "TestApp Debug",
        "type": "coreclr",
        "request": "launch",
        "preLaunchTask": "build",
        "program": "${workspaceRoot}/src/TestApp/bin/Debug/netcoreapp1.0/TestApp.dll",
        "args": [],
        "cwd": "${workspaceRoot}/src/TestApp",

So I can debug multiple projects in a solution in visual studio code.

Upvotes: 5

DaniCE
DaniCE

Reputation: 2421

In vs code v1.10.2 you can work with multiple projects if you have a solution in the root. The solution should be in visual studio 15 format.

This solution could be defined using these new dotnet cli commands:

dotnet new sln //creates a new solution in folder
dotnet sln add <path to the csproj file>  //adds project to solution

Also to add a reference to some project B in project A, you should move to project A folder and execute:

dotnet add reference <path to the project B csproj file>  

Upvotes: 3

Hypnobrew
Hypnobrew

Reputation: 1140

If you have for example multiple runnable projects i.e. a client/server:

Project structure:

Project
--|Server
----Program.cs
----project.json
--|Client
----Program.cs
----project.json

I have two differents configurations in launch.json

"configurations": [
    {
        "name": "Server",
        "type": "coreclr",
        "request": "launch",
        "preLaunchTask": "build",
        "program": "${workspaceRoot}/Server/bin/Debug/netcoreapp1.1/Server.dll",
        "args": [],
        "cwd": "${workspaceRoot}/Server",
        "stopAtEntry": false,
        "externalConsole": false
    },
    {
        "name": "Client",
        "type": "coreclr",
        "request": "launch",
        "preLaunchTask": "build",
        "program": "${workspaceRoot}/Client/bin/Debug/netcoreapp1.1/Client.dll",
        "args": [],
        "cwd": "${workspaceRoot}/Client",
        "stopAtEntry": false,
        "externalConsole": false
    }]

And the tasks.json

{
    "version": "0.1.0",
    "command": "dotnet",
    "isShellCommand": true,
    "args": [],
    "tasks": [
      {
        "taskName": "build",
        "args": ["Server","Client"],
        "isBuildCommand": true,
        "showOutput": "silent",
        "problemMatcher": "$msCompile"
      }
   ]
}

This will setup two debuggable projects, and the PreBuildTask will compile the projects specified in the args field in the tasks.json, before start debugging. (Server and Client projects)

In this case I have set the projects to bind to different ports, to be able to debug them at the same time, by specify that in WebHostBuilder in Program.cs

.UseUrls("http://localhost:5002")

This can be changed in launchsettings.json if you are using Visual Studio, NOT VsCode.

Upvotes: 0

Related Questions