Raj
Raj

Reputation: 237

Error on dotnet watch run in Windows

I am getting following Error on running 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

I confirmed there is nothing wrong json file. Adding my project files if that helps. This may be irrelevant but I am not sure where to look.

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

Any suggestion where to start looking?

Upvotes: 2

Views: 3011

Answers (1)

Ben
Ben

Reputation: 35613

I suspect that either the file is empty, or it begins with a UTF-8 BOM.

The clue is here:

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

The thing it is complaining about is invisible : ''

So possibly it is either nothing was found where something is expected, or it is an invisible character of some sort, which would include '\uFEFF', the BOM.

For reasons of insanity, JSON files are explicitly forbidden from using a BOM at the start, even though that codepoint can appear anywhere else in the file.

Upvotes: 6

Related Questions