JasCav
JasCav

Reputation: 34632

config.json Not Being Found on ASP.NET Core Startup in Debug

I have created a basic WebAPI project via Yeoman (note: I have this same issue with a "real" project I am working on, but yo demonstrates the problem as well) targeted at netcoreapp1.0 on OSX.

On the command line, it restores, builds, and runs fine via dotnet restore, dotnet build, dotnet run.

When I am in Visual Studio Code and use Debug, however, I always receive the error: "The configuration file 'config.json' was not found and is not optional." which points at the first line of my Main method.

Here is my Program.cs entry point:

public static void Main(string[] args)
{            
    var host = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();

    host.Run();
}

Here is project.json:

{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Configuration.CommandLine": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "Areas/**/Views",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  },

  "tooling": {
    "defaultNamespace": "WebAPIApplication"
  }
}

Adding Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace WebAPIApplication
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("config.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"config.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseMvc();
        }
    }
}

If any other information is required, please let me know and I would be happy to add it.

As Gerardo suggested, printing out Directory.GetCurrentDirectory() I get two different outputs.

From command line (which works): ~/Projects/Path/WebApiApplication/ From debug in VSCode: ~/Projects/Path

What I also noticed is that output is going to ~/Projects/Path/WebApiApplication/bin/Debug/netcoreapp1.0/ but no files are going with it (including config.json).

Upvotes: 4

Views: 2836

Answers (2)

Mr_LinDowsMac
Mr_LinDowsMac

Reputation: 2702

Move .vscode folder INSIDE of "YourSolution/src/YourProject" directory, by mistake opened the project in VSCode in YourSolution directory and by tweaking configuration like the suggested answer ran the application, but with configuration problems.

So in VSCode you have to open the project from inside YourProject and default config will run properly.

Upvotes: 0

Gerardo Grignoli
Gerardo Grignoli

Reputation: 15167

It seems like dotnet run is being run standing on the project folder and works correctly, but VS Code debug-mode is not.

This is configured in VS Code in a config file .vscode\launch.json. Find the line that says:

       "cwd": "${workspaceRoot}",

and change it to:

        "cwd": "${workspaceRoot}/MyProjectFolder",

You need to change the startup folder (cwd) so that it matches the location of your project.json/config.json/ etc.

More Info here: Visual Studio Code Launch Configurations

Upvotes: 5

Related Questions