Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to read appsettings.json when debugging .net core webapi in vscode

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.

like image 874
Konzy262 Avatar asked Sep 15 '20 10:09

Konzy262


People also ask

How do I debug a .NET Core code in Visual Studio?

Open the Debug view by selecting the Debugging icon on the left side menu. Select the green arrow at the top of the pane, next to . NET Core Launch (console). Other ways to start the program in debugging mode are by pressing F5 or choosing Run > Start Debugging from the menu.

How do I debug API code in Visual Studio?

Switch to the Run and Debug view (Ctrl+Shift+D) and select the create a launch. json file link. VS Code will let you select an "debugger" in order to create a default launch configuration. Pick "Mock Debug".


1 Answers

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...

enter image description here

like image 100
Nilo Alan Avatar answered Oct 07 '22 14:10

Nilo Alan