在vscode中调试.net Core WebAPI时无法读取appsettings.json

问题描述

在vscode中进行调试时,我无法读取Webapi项目的appsettings.json文件中的Startup.cs。它们全部返回为空/空。

我的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","args": [
                "publish",{
            "label": "watch","args": [
                "watch","run","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的services方法中调试以下代码时

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

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

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

Connection string = 

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

这里可能是什么问题?

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)