在 vscode 中调试 .net core webapi 时无法读取 appsettings.json

Kon*_*262 3 visual-studio-code asp.net-core-webapi vscode-debugger

当vscode调试我无法读取appsettings.json我的Startup.cs我的WebAPI项目的文件。它们都返回为空/空。

launch.json的 webapi 配置是通过添加.NET Core Launch (console)配置生成的。我也尝试添加.NET Core Launch (web)配置,但出现同样的问题。

然后我唯一需要更改的是"program"指向项目文件夹中正确 dll的设置。

这是我的 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"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

这是我的 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"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

这是我的appsettings.jsonappsettings.Development.json文件...

{
  "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": "*"
}
Run Code Online (Sandbox Code Playgroud)

当我尝试在我的服务方法中调试以下代码时 Startup.cs

var connectionString = Configuration["ConnectionStrings:DefaultConnection"];

Console.WriteLine($"Connection string = {connectionString}");
Run Code Online (Sandbox Code Playgroud)

我只是将以下内容记录到调试终端

Connection string = 
Run Code Online (Sandbox Code Playgroud)

中的所有其他设置appsettings.json也没有返回。使用时会返回正确的设置dotnet run

这里可能是什么问题?

请注意,我的工作目录中有一个解决方案文件,然后我有一个名为SATestUtils.APISATestUtils.API.csproj文件夹,其中包含我的 webapi 项目的文件。

小智 7

我遇到了同样的情况,对我来说,有效的是之前评论过的:

将 launch.json 中 'cwd' 属性的值更改为项目目录的值。

{ ... "cwd": "${workspaceFolder}" }

{ ... "cwd": "${workspaceFolder}/SATestUtils.API" }

Bemm 的所有功劳...

在此处输入图片说明