DEV Community

Cover image for Launch .Net Core with VSCode for starters
Arjun Shetty
Arjun Shetty

Posted on

5 4

Launch .Net Core with VSCode for starters

To use VSCode for .net core you would need Omnisharp extension so that we can debug our application just like as in Visual Studio.
Using the dotnet new command we create a web and an API project

dotnet new webapp appname.web
dotnet new webapi appname.api

Create a solution so that it wraps all the projects in one place

dotnet new sln appname
dotnet sln add appname.web appname.api

After building the Web and the API projects we need to create the launch.json which VScode refers to build and debug the application. For a complete reference on doing these projects consider this great resource.

You can always use VSCode terminal to run these above commands as well, also note you can open multiple terminals the same VSCode instance like this.

Setup the launch.json by clicking on add configuration and select .Net core web app. we will need to add two such configurations one for web and another for web api. 
For web api you need to launch the browser on running the app, disable 

launchBrowser.enabled: false

we need to update the Url's for both our applications with different ports

env.ASPNETCORE_URLS: "https://localhost:5001"

The build task for the project is defined in tasks.json to which the preLaunchTask references. In below example launch.json I have created two tasks to be executed before running the application. This is how we execute multiple tasks we need to chain each task with the first task name mentioned in preLaunchTask and the rest in tasks.json dependsOn property.

F5 and launch with debug mode on!

Below are the complete launch.json and tasks.json

{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (web-api)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "buildapi",
"program": "${workspaceFolder}/serviceflow.api/bin/Debug/netcoreapp2.2/serviceflow.api.dll",
"args": [],
"cwd": "${workspaceFolder}/serviceflow.api",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart",
"launchBrowser": {
"enabled": false,
"args": "${auto-detect-url}",
"windows": {
"command": "cmd.exe",
"args": "/C start ${auto-detect-url}"
},
"osx": {
"command": "open"
},
"linux": {
"command": "xdg-open"
}
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_URLS": "https://localhost:5001"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "buildweb",
"program": "${workspaceFolder}/serviceflow.web/bin/Debug/netcoreapp2.2/serviceflow.web.dll",
"args": [],
"cwd": "${workspaceFolder}/serviceflow.web",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart",
"launchBrowser": {
"enabled": true,
"args": "${auto-detect-url}",
"windows": {
"command": "cmd.exe",
"args": "/C start ${auto-detect-url}"
},
"osx": {
"command": "open"
},
"linux": {
"command": "xdg-open"
}
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_URLS": "https://localhost:6001"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
},
]
}
view raw launch.json hosted with ❤ by GitHub
{
"version": "2.0.0",
"tasks": [
{
"label": "cleanweb",
"command": "dotnet",
"type": "process",
"args": [
"clean",
"${workspaceFolder}/serviceflow.web/serviceflow.web.csproj"
],
"problemMatcher": "$msCompile"
},
{
"label": "buildweb",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/serviceflow.web/serviceflow.web.csproj"
],
"problemMatcher": "$msCompile",
"dependsOn":["cleanweb"]
},
{
"label": "cleanapi",
"command": "dotnet",
"type": "process",
"args": [
"clean",
"${workspaceFolder}/serviceflow.api/serviceflow.api.csproj"
],
"problemMatcher": "$msCompile"
},
{
"label": "buildapi",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/serviceflow.api/serviceflow.api.csproj"
],
"problemMatcher": "$msCompile",
"dependsOn":["cleanapi"]
}
]
}
view raw tasks.json hosted with ❤ by GitHub

-originally posted on Bitsmonkey

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (3)

Collapse
 
haizzten profile image
haizzten

I'm here to say thank to you.
By adding "clean" task in Task.json (not just defaults tasks created by Omnisharp), I've finally been able to resolve the irritated "The breakpoint will not currently be hit. The source code is different from the original version" problem! Phew.

Collapse
 
lautarolobo profile image
Lautaro Lobo

I'll try it. I'm in a new job with lots of .net MVC stuff

Collapse
 
xarjunshetty profile image
Arjun Shetty

Good Luck to you (y). This is probably the best time to be working on MS tech stack.