在Windows上运行dotnet watch时出错

时间:2017-07-24 05:09:16

标签: c# .net asp.net-core

我在运行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文件没有错。如果有帮助,添加我的项目文件。这可能无关紧要,但我不确定在哪里看。

appsettings.json:

{
  "ASPNETCORE_ENVIRONMENT": "Development",
  "ConnectionStrings": {
    "Default": "Server=localhost; database=TestProject; Integrated Security=True"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

Startup.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();
}

有什么建议从哪里开始寻找?

1 个答案:

答案 0 :(得分:6)

我怀疑该文件是空的,或者它以UTF-8 BOM开头。

线索在这里:

Error on line number '0': ''. -

它抱怨的是看不见的:''

所以可能没有找到任何预期的东西,或者它是某种不可见的角色,包括'\uFEFF',BOM。

出于精神错乱的原因,明确禁止JSON文件在开始时使用BOM,即使该代码点可以出现在文件的任何其他位置。