【问题标题】:Unable to read appsettings.json when debugging .net core webapi in vscode在 vscode 中调试 .net core webapi 时无法读取 appsettings.json
【发布时间】:2020-09-15 10:27:29
【问题描述】:

在 vscode 中调试时,我无法读取我的 webapi 项目的 Startup.cs 文件中的 appsettings.json。它们都返回为空/null。

我的 webapi 的 launch.json 配置是通过添加 .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 的文件夹,其中包含我的 webapi 项目的 SATestUtils.API.csproj 文件。

【问题讨论】:

  • 在终端中使用dotnet builddotnet run 启动应用程序时,您能读取appsettings.json 吗?
  • 是的,它们已正确加载
  • 看起来cwd 的值与您的项目目录不同。如果您将该值从 ${workspaceFolder} 更改为 ${workspaceFolder}/SATestUtils.API 会怎样?
  • 我遇到了同样的问题,但除了修复 cwd 条目之外,我还必须将 "console": "internalConsole" 添加到 launch.json

标签: visual-studio-code asp.net-core-webapi vscode-debugger


【解决方案1】:

我经历过同样的案例,对我来说,有效的是之前的评论:

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

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

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

Bemm 的所有功劳...

【讨论】:

  • 非常感谢。
  • 非常有用的提示,谢谢!
  • 这非常有用,非常感谢。
猜你喜欢
  • 1970-01-01
  • 2021-10-14
  • 1970-01-01
  • 1970-01-01
  • 2018-05-05
  • 2018-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多