visual-studio-code - 在 vscode 中调试 .net core webapi 时无法读取 appsettings.json
问题描述
在 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"
}
]
}
这是我的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
,其中包含SATestUtils.API.csproj
我的 webapi 项目的文件。
解决方案
推荐阅读
- silverstripe - 您如何在 silverstripe CMS 上过滤 CSV 导出?
- php - 在 move_uploaded_file 中传递根路径
- apache - .htaccess redirect if certain parameters are met
- python - 不明白为什么会出现这个浮动问题
- spring-boot - 为什么我必须禁用 Javers 的模式管理才能初始化我的应用程序?
- python - 正确索引大型时间序列数据集以进行复杂查询
- git - gerrit 绕过现有的 git 服务器端钩子
- python - 如何链接到 S3 文件下载
- c++ - 穿过嵌套地图
- python - 如何将字符串更改为列表