在使用VS Code运行asp.net core时,Configuration.GetConnectionString返回null,但在Visual Studio上却正常。

21

这是我的appsettings.json文件。

{
  "ConnectionStrings": {
    "DefaultConnection": "Host=localhost;Port=5432;Database=db;User ID=postgres;Password=root"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

这是我检索连接字符串的方法:

// Only works when run through visual studio not on vs code
Configuration.GetConnectionString("DefaultConnection")

我的launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (web)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${workspaceRoot}\\src\\Chlx\\bin\\Debug\\netcoreapp1.0\\Chlx.dll",
            "args": [],
            "cwd": "${workspaceRoot}",
            "stopAtEntry": false,
            "internalConsoleOptions": "openOnSessionStart",
            "launchBrowser": {
                "enabled": true,
                "args": "${auto-detect-url}",
                "windows": {
                    "command": "cmd.exe",
                    "args": "/C start ${auto-detect-url}"
                },
                "osx": {
                    "command": "open"
                },
                "linux": {
                    "command": "xdg-open"
                }
            },
            "env": {
                "ASPNETCORE_ENVIRONMENT": "Development"
            },
            "sourceFileMap": {
                "/Views": "${workspaceRoot}/Views"
            }
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command.pickProcess}"
        }
    ]
}

我的tasks.json

{
    "version": "0.1.0",
    "command": "dotnet",
    "isShellCommand": true,
    "args": [],
    "tasks": [
        {
            "taskName": "build",
            "args": [
                "${workspaceRoot}\\src\\Chlx\\project.json"
            ],
            "isBuildCommand": true,
            "problemMatcher": "$msCompile"
        }
    ]
}

你知道如何修复这个问题吗?


你是如何通过 VS Code“运行”它的? - DavidG
我正在通过调试窗格“ .Net Core Launch (Web)”运行它。我可以设置断点,但在vs code上获取连接字符串的方法返回null。 - bman
你如何在启动时设置配置? - agua from mars
请检查launchSettings.json中的environmentVariables。 - adem caglin
请大家检查我的编辑。我添加了一个launch.json和tasks.json文件。 - bman
2个回答

32

您应该从根目录(解决方案级别),而非项目级别运行程序。请在launchsettings.json中将 "cwd" 更改为 ${workspaceRoot}\src\Chlx\。


4
你很棒。 - bman
将applicationsettings.json添加到根文件夹中。这对我来说解决了问题 :) - Timo Hermans
1
在ASP.NET Core 2.0中,我有两个项目:WebApi和DataAccess。WebApi包含appsettings.json。在包含这两个项目的父目录中,它是解决方案文件。我遇到了同样的问题并且:将appsettings.json复制到解决方案级别或从WebApi项目运行应用程序,其中任何一个都解决了我的问题。 - hmadrigal

3

配置属性定义在Startup类中,通过依赖注入进行初始化。以下是使用dotnet new mvc -au Individual创建的项目的示例:

  public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接