Unable to read appsettings.json when debugging .net core webapi in vscode - visual-studio-code

When debugging in vscode I'm unable to read the appsettings.json in my Startup.cs file of my webapi project. They all return as empty/null.
My launch.json configuration for the webapi was generated by adding the .NET Core Launch (console) configuration. I also tried adding the .NET Core Launch (web) configuration but the same problem occurs.
The only thing I then had to change was the "program" setting to point to the correct dll in my project folder.
Here is my launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/SATestUtils.API/bin/Debug/netcoreapp3.1/SATestUtils.API.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"console": "internalConsole"
}
]
}
And here is my tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/SATestUtils.Api/SATestUtils.API.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/SATestUtils.Api/SATestUtils.API.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/SATestUtils.Api/SATestUtils.API.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
Here is my appsettings.json and appsettings.Development.json file...
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=SaTestUtils"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"StorageAccountName": "devstoreaccount1",
"AzureAD": {
"Instance": "https://login.microsoftonline.com",
"Domain": "[Domain]",
"TenantId": "[TenantId]",
"ClientId": "[ClientId]",
"Scope": "[Scope]"
},
"AllowedHosts": "*"
}
When I attempt to debug the following code in my services method in Startup.cs
var connectionString = Configuration["ConnectionStrings:DefaultConnection"];
Console.WriteLine($"Connection string = {connectionString}");
I simply get the following logged to the debug terminal
Connection string =
All the other settings in appsettings.json are also not returning. The correct settings are returned when using dotnet run.
What could be the issue here?
Note that I have a solution file in my working directory, then I have a folder called SATestUtils.API that contains the SATestUtils.API.csproj file for my webapi project.

I experienced the same case, for me, what worked was what has been commented before:
Change the value of 'cwd' property in launch.json to the value of the project directory.
{
...
"cwd": "${workspaceFolder}"
}
to
{
...
"cwd": "${workspaceFolder}/SATestUtils.API"
}
All the credits to Bemm...

Did you try to specify the asp net environment at the launch.json?
Something like this:

Related

vscode cpp build tasks.json type:"cppbuild" error

VSCode : 1.61.0
Linux: Ubuntu 20.04.3 LTS
I'm trying to build c++ program using by VSCode.
when the run build, VSCode showing this message.
I know build tasking need matching launch.json file's preLaunchTask with tasks.json file's label. and I'm already set it.
tasks.json
{
"tasks": [
{
"type": "cppbuild", // but "shell" is working
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
The problem is, if I change the "type" value in tasks.json, from shell to cppbuild, It doesn't work and show the error message like to.
showing error message when the run build
what is different "cppbuild" and "shell"? and What should I do how using "cppbuild" value?
Te values of cppbuild are fine. Try changing in launch.json the value:
"preLaunchTask": "C/C++: g++ build active file",
to:
"preLaunchTask": "cppbuild",
The difference between shell and cppbuild is that the first open the BASH and runs a command you write, the second, cppbuild, runs an specific binary (or program).

VS CODE / MSBUILD : error MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file

I've tried many different ways but I get the error MSBUILD : error MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file. in Visual Studio code when trying to run my Net Core.
I think I have this configured correctly. The project builds with no errors. I even tried building a new empty project and still get the error.
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/API/API/bin/Debug/net5.0/API.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
}
]
}
Tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "shell",
"args": [
"build",
// Ask dotnet build to generate full paths for file names.
"/property:GenerateFullPaths=true",
// Do not generate summary otherwise it leads to duplicate errors in Problems panel
"/consoleloggerparameters:NoSummary"
],
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
}

How to let VS Code skip source files with no changes since last compiling?

Eclipse CDT can automatically compile all the changed source files (if there are) in the project directory and link them. However the default "C/C++: g++.exe build active file" task would always compile the file even there was no change since last compile. Yes, it could be done by make file. Besides that, do VSC or its extensions provide some easier way to do so?
Here is how my tasks.json looks like:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "\"${config:task.gccpath}/g++.exe\"",
"args": [
"-Wall",
"-g",
"${file}",
"-o",
"${workspaceFolder}/Debug/${relativeFileDirname}/${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${config:task.gccpath}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: ${config:task.gccpath}/g++.exe"
}
]
}
And here is how my launch.json looks like
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/Debug/${relativeFileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/IO/${relativeFileDirname}/",
"environment": [],
"console": "externalTerminal",
"MIMode": "gdb",
"miDebuggerPath": "${config:task.gccpath}/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}

VS Code -- Unexpected Output Format /ERROR\

Im new to stackoverflow and VS Code. When I tried to debug my code I got error message, and couldn't fix it.
I uploaded the error message
and;
here is mylaunch.json:
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information,visit: https://go.microsoft.com/fwlink/?linkid=830387
{
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Etkin dosyayı derle ve dosyada hata ayıkla",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "gdb için düzgün yazdırmayı etkinleştir",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
and task.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\TDM-GCC-64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\TDM-GCC-64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Generated task by Debugger"
}
],
"version": "2.0.0"
}
my main.cpp file is simply printes "Hello"
I installed MinGW and its directory is : C:\MinGW<
I hope there is someone who can help me.
I encountered the same problem recently. It seems that the auto-generated tasks.json is not set correctly and the executable is created in the root directory. This can be fixed by changing the type from "cppbuild" to "shell".
{
"tasks": [
{
"type": "shell",
...
}
],
"version": "2.0.0"
}

Chain pre-launch tasks in VS Code

Is it possible to invoke more than one pre-launch task using VS Code?
I try to restore packages then build then run but I can only get to configure build.
My launch.json:
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}/src/myProject/bin/Debug/netcoreapp1.0/myProject.dll",
"args": [],
"cwd": "${workspaceRoot}/src/myProject",
"stopAtEntry": false,
"externalConsole": false
},
My tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "dotnet",
"isShellCommand": true,
"args": [],
"tasks": [
{
"taskName": "build",
"args": [
"./**/project.json"
],
"isBuildCommand": true,
"showOutput": "always",
"problemMatcher": "$msCompile"
}
]
}
so I tried to specify the dotnet restore command however it does not work.
I know this is long over due. However, I think I figured out the solution. Steps I did.
Create a workspace to include all the projects in the workspace.
Go to Run and Debug, and click on "Add Config (workspace)
the format is the following:
{
"folders": [
{
"path": "project1"
},
{
"path": "project2"
},
],
"launch": {
"version": "0.2.0",
"compounds": [{
"name": "Chain Stars",
"configurations": [
"ConfigurationName1",
"ConfigurationName2",
]
}]
}
}
ConfigurationName1, and ConfigurationName2 is the profile name you would like to put them in sequence to launch your website.
4. Save the profile. In this case. "Chain Stars" is going to show up in the profile name for you to run it. Let me know if you have any questions.