-
Notifications
You must be signed in to change notification settings - Fork 202
System.NullReferenceException for invalid appsettings.json #476
Description
This took me a while to track down...
As I was porting a project from dnx to dotnet I came across this issue.
I missed a closing curly brace "}" in the appsettings.json file. VS does warn if I had the file opening, however, I didn't think to look there until after walking the code.
The invalid json throws a Null Reference exception. I feel that there should be some json validation further upstream, maybe JsonConfigurationProvider.Load and throw an appropriate invalid Json exception there.
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider.RetrieveErrorContext(JsonReaderException e, IEnumerable1 fileContent) at Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider.Load(Stream stream) at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload) at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList
1 providers)
at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
at XXXX.Startup..ctor(IHostingEnvironment env) in XXXX\Startup.cs:line 45
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.Extensions.Internal.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
at Microsoft.AspNetCore.Hosting.Internal.StartupLoader.LoadMethods(IServiceProvider services, Type startupType, String environmentName)
at Microsoft.AspNetCore.Hosting.WebHostBuilderExtensions.<>c__DisplayClass1_0.b__1(IServiceProvider sp)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureStartup()
at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()