【发布时间】:2016-12-23 12:39:21
【问题描述】:
我通过 Yeoman 创建了一个基本的 WebAPI 项目(注意:我正在处理的“真实”项目也有同样的问题,但yo 也演示了这个问题)针对 OSX 上的netcoreapp1.0。
在命令行上,它通过dotnet restore、dotnet build、dotnet 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