Configuration.GetConnectionString returns null when running asp.net core on VS Code but fine on Visual Studio

15,100

Solution 1

You run the program from the root (solution level) and not from the project. Change your "cwd" in the launchsettings.json to ${workspaceRoot}\src\Chlx\

Solution 2

The Configuration property is defined in the Startup class and it is initialized thru dependency injection. Here is the sample for a project created with dotnet new mvc -au Individual

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

        public IConfiguration Configuration { get; }
Share:
15,100
bman
Author by

bman

Updated on June 05, 2022

Comments

  • bman
    bman almost 2 years

    Here is my appsettings.json file

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

    This is how I retrieve the connection string:

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

    My 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}"
            }
        ]
    }
    

    My tasks.json

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

    Do you know how to fix this?

  • bman
    bman over 7 years
    you are awesome
  • Timo Hermans
    Timo Hermans about 7 years
    Or add applicationsettings.json to the root folder. That fixed it for me :)
  • hmadrigal
    hmadrigal over 6 years
    In ASP.NET Core 2.0 I have two projects: WebApi and DataAccess. The WebApi contains the appsettings.json. In the parent directory (containing the two projects) it is the solution file. I was experincing the same issue and: Copying the appsettings.json at the level of the solution or running the app from the WebApi project, any of those resolved my issue