首页 > 解决方案 > 在 vscode 中调试 .net core webapi 时无法读取 appsettings.json

问题描述

在 vscode 中调试时,我无法读取appsettings.jsonStartup.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"
        }
    ]
}

这是我的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"
        }
    ]
}

这是我的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": "*"
}

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

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

Console.WriteLine($"Connection string = {connectionString}");

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

Connection string = 

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

这里可能是什么问题?

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

标签: visual-studio-codeasp.net-core-webapivscode-debugger

解决方案


我经历了同样的情况,对我来说,有效的是之前评论过的内容:

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

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

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

Bemm的所有学分......

在此处输入图像描述


推荐阅读