【发布时间】: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.json 和appsettings.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 build和dotnet run启动应用程序时,您能读取appsettings.json吗? -
是的,它们已正确加载
-
看起来
cwd的值与您的项目目录不同。如果您将该值从${workspaceFolder}更改为${workspaceFolder}/SATestUtils.API会怎样? -
我遇到了同样的问题,但除了修复
cwd条目之外,我还必须将"console": "internalConsole"添加到launch.json
标签: visual-studio-code asp.net-core-webapi vscode-debugger