我在运行 dotnet watch run 时遇到以下错误。
Unhandled Exception: System.FormatException: Could not parse the JSON file.
Error on line number '0': ''. -
--> Newtonsoft.Json.JsonReaderException: Error reading JObject from
JsonReader. Path '', line 0, position 0
at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader, JsonLoadSettings
settings) at
Microsoft.Extensions.Configuration.Json.JsonConfigurationFileParser.Parse(Stream input)
at Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider.Load(Stream stream)
--- End of inner exception stack trace ---
at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)
at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)
at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
at WebApplicationBasic.Startup..ctor(IHostingEnvironment env) in \Microservices\TestProject\Startup.cs:line 25
我确认json文件没有错。如果有帮助,请添加我的项目文件。这可能无关紧要,但我不确定去哪里找。
应用设置.json:
{
"ASPNETCORE_ENVIRONMENT": "Development",
"ConnectionStrings": {
"Default": "Server=localhost; database=TestProject; Integrated Security=True"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
启动.cs:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IServiceProvider ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<TestDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Default")));
services.AddMvc();
// services.AddMvcCore()
// .AddApiExplorer();
return services.BuildServiceProvider();
}
有什么建议从哪里开始寻找吗?
最佳答案
我怀疑文件是空的,或者以 UTF-8 BOM 开头。
线索在这里:
Error on line number '0': ''. -
它提示的东西是不可见的:''
所以可能是在预期的地方找不到任何东西,或者它是某种不可见的字符,其中包括 '\uFEFF'
,即 BOM。
出于疯狂的原因,明确禁止 JSON 文件在开始时使用 BOM,即使该代码点可以出现在文件中的任何其他位置。
关于c# - 在 Windows 中运行 dotnet watch 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45273010/