c# - 在 Windows 中运行 dotnet watch 时出错

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

应用设置.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/

相关文章:

c# - 我可以阻止在我的机器人中注册 DeleteProfileScorable 吗?

c# - Windows 版本在设计中显示出真正的值(value),但在我运行程序时显示出不同的值(value)

c# - 将不同的泛型存储在单个可枚举中

asp.net - 在本地解析Docker主机名时HttpClient花费太多时间

c# - 找不到名称为 WCF 的端点元素

c# - 如何对中间层 .NET 应用程序类/方法进行版本控制

.net - SortedList 与 SortedDictionary 与 Sort()

c# - 在运行时添加新页面

c# - 如何在 ASP.NET Web API 核心中全局启用 CORS

c# - 将用户添加到现有角色时任务被取消