【问题标题】:config.json Not Being Found on ASP.NET Core Startup in Debug在调试的 ASP.NET Core 启动上找不到 config.json
【发布时间】:2016-12-23 12:39:21
【问题描述】:

我通过 Yeoman 创建了一个基本的 WebAPI 项目(注意:我正在处理的“真实”项目也有同样的问题,但yo 也演示了这个问题)针对 OSX 上的netcoreapp1.0

在命令行上,它通过dotnet restoredotnet builddotnet run 恢复、构建和运行良好。

但是,当我在 Visual Studio Code 中并使用 Debug 时,我总是收到错误:"The configuration file 'config.json' was not found and is not optional.",它指向我的 Main 方法的第一行。

这是我的Program.cs 入口点:

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

    host.Run();
}

这里是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"
  }
}

添加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();
        }
    }
}

如果需要任何其他信息,请告诉我,我很乐意添加。

正如 Gerardo 建议的那样,打印出 Directory.GetCurrentDirectory() 我得到两个不同的输出。

从命令行(有效):~/Projects/Path/WebApiApplication/ 来自 VSCode 中的调试:~/Projects/Path

我还注意到,输出将发送到 ~/Projects/Path/WebApiApplication/bin/Debug/netcoreapp1.0/,但没有文件随之发送(包括 config.json)。

【问题讨论】:

  • 您的 Startup 类是否包含类似 new ConfigurationBuilder().AddJsonFile("Config.json") 的内容?
  • @GerardoGrignoli 确实如此,是的。我会将 Startup.cs 添加到我的问题中。
  • ConfigurationBuilder 使用相对于.SetBasePath(env.ContentRootPath 的路径,该路径来自Main.UseContentRoot(Directory.GetCurrentDirectory())。那么:VS Code 调试会在不同的目录上启动您的应用程序吗?请将此Console.WriteLine(Directory.GetCurrentDirectory()) 添加为Main 的第一行,并比较'dotnet run' 和VS Code Debug 之间的输出。
  • @GerardoGrignoli 谢谢。绝对提供了一些有趣的信息。在我的问题中详细说明。

标签: c# json asp.net-web-api asp.net-core


【解决方案1】:

似乎dotnet run 正在项目文件夹上运行并且工作正常,但VS Code debug-mode 不是。

这是在配置文件.vscode\launch.json 中的VS Code 中配置的。 找到显示以下内容的行:

       "cwd": "${workspaceRoot}",

并将其更改为:

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

您需要更改启动文件夹 (cwd),使其与您的 project.json/config.json/ 等的位置相匹配。

更多信息在这里:Visual Studio Code Launch Configurations

【讨论】:

  • 当您提到.vscode\launch.json 时,我意识到 - 我打开的文件夹比我拥有的许多项目高出一步(有些是其他项目的库)。这意味着有一个顶级.vscode\launch.json。如果我只打开我要使用的项目,Debug 效果很好。我想这让我想到了另一个问题——我如何在 VSCode 中正确引入属于项目一部分的其他库?无论哪种方式,将您的答案标记为好,因为它确实让我走上了正确的道路。谢谢!
  • 您是否尝试过按照我在答案中的建议编辑launch.json 文件?
【解决方案2】:

"YourSolution/src/YourProject" 目录中移动 .vscode 文件夹,错误地在 YourSolution 目录中的 VSCode 中打开项目,并通过调整配置(如建议的答案)运行应用程序,但配置问题。

因此,在 VSCode 中,您必须从 YourProject 中打开项目,默认配置才能正常运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多