22

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?

5
  • How exactly are you "running" it through VS Code? Commented Sep 22, 2016 at 0:33
  • I am running it through the debug pane ".Net Core Launch (Web)" . I can set breakpoints and the method to get the connection string returns null on vs code Commented Sep 22, 2016 at 6:21
  • How do you setup configuration in your startup? Commented Sep 22, 2016 at 6:25
  • Check environmentVariables in launchSettings.json. Commented Sep 22, 2016 at 6:40
  • Please check my edit guys. I added a launch.json and tasks.json Commented Sep 22, 2016 at 7:52

2 Answers 2

35

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\

Sign up to request clarification or add additional context in comments.

3 Comments

you are awesome
Or add applicationsettings.json to the root folder. That fixed it for me :)
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
3

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; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.