Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

System.NullReferenceException for invalid appsettings.json #476

Closed
meikeric opened this issue Jul 7, 2016 · 7 comments
Closed

System.NullReferenceException for invalid appsettings.json #476

meikeric opened this issue Jul 7, 2016 · 7 comments
Assignees

Comments

@meikeric
Copy link

meikeric commented Jul 7, 2016

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(IList1 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()

@vsg24
Copy link

vsg24 commented Jul 28, 2016

Agreed 1000000%. The current exception doesn't give any useful info, it should be replaced with a more meaningful one.

@giggio
Copy link

giggio commented Jul 29, 2016

Just had this problem now. Really cryptic. This should be handled better, definitely.

@HaoK
Copy link
Member

HaoK commented Aug 23, 2016

74c68cd

@HaoK HaoK closed this as completed Aug 23, 2016
@giggio
Copy link

giggio commented Aug 24, 2016

Looks much better now. Thanks @HaoK .

@analogrelay
Copy link

It looks like we still throw a NullReferenceException (or just return null values) if reloadOnChange is true and you change the file to have invalid JSON. The reload-on-change behavior does continue to work though, so when you correct the error the problem is resolved.

Thoughts on fixing that @HaoK @divega ? The scenario is: With a JSON config file with reloadOnChange set to true, change the file to be invalid JSON, attempt to read from config, get null values (but no error in the page, except those caused by the now-null values, or log messages), change back to valid JSON, get correct values again. I can see the value in emitting null values, but we should probably at least log the parse error somewhere.

@divega
Copy link

divega commented Oct 20, 2016

@HaoK would configurationBuilder.SetFileLoadExceptionHandler() help in this case? If yes, I think we could consider wiring up something that logs the error in the default templates. Configuration cannot use logging directly though.

@HaoK
Copy link
Member

HaoK commented Oct 25, 2016

Yeah that's why we added that feature to make it possible to log at least for situations like this.

@HaoK would configurationBuilder.SetFileLoadExceptionHandler() help in this case? If yes, I think we could consider wiring up something that logs the error in the default templates. Configuration cannot use logging directly though.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants