Configuration system failed to initialize ---> System.NullReferenceException: Object reference not set to an instance of an object.

Sal Datoccio 96 Reputation points
2021-02-04T13:00:09.907+00:00

After months running without any issues/without any changes to the application/server, one of our ASP.NET (MVC/.NET Framework 4.x) applications failed with "Configuration system failed to initialize" every time that access to the configuration system was needed to instantiate a DBContext.

We restarted the application on IIS and the issue is gone, but we are wondering what could cause the following error:

Configuration system failed to initialize ---> System.NullReferenceException: Object reference not set to an instance of an object.

    could not instantiate KB.EntityFramework.KBDbContext ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.NullReferenceException: Object reference not set to an instance of an object.
       at System.Configuration.ClientConfigurationHost.GetStreamName(String configPath)
       at System.Configuration.ClientConfigurationHost.get_IsAppConfigHttp()
       at System.Configuration.ClientConfigurationSystem..ctor()
       at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
       --- End of inner exception stack trace ---
       at System.Configuration.ConfigurationManager.PrepareConfigSystem()
       at System.Configuration.ConfigurationManager.GetSection(String sectionName)
       at System.Configuration.ConfigurationManager.get_AppSettings()
       at System.Data.Entity.Core.Objects.ObjectContext..ctor(EntityConnection connection, Boolean isConnectionConstructor, ObjectQueryExecutionPlanFactory objectQueryExecutionPlanFactory, Translator translator, ColumnMapFactory columnMapFactory)
       at System.Data.Entity.Infrastructure.DbCompiledModel.<.cctor>b__2(EntityConnection c)
       at System.Data.Entity.Infrastructure.DbCompiledModel.CreateObjectContext[TContext](DbConnection existingConnection)
       at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
       at System.Data.Entity.Internal.InternalContext.Initialize()
       at System.Data.Entity.Internal.InternalContext.ForceOSpaceLoadingForKnownEntityTypes()
       at System.Data.Entity.DbContext.System.Data.Entity.Infrastructure.IObjectContextAdapter.get_ObjectContext()
       at KB.EntityFramework.KBDbContext..ctor(String nameOrConnectionString)

  System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.NullReferenceException: Object reference not set to an instance of an object.
           at System.Configuration.ClientConfigurationHost.GetStreamName(String configPath)
           at System.Configuration.ClientConfigurationHost.get_IsAppConfigHttp()
           at System.Configuration.ClientConfigurationSystem..ctor()
           at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
           --- End of inner exception stack trace ---
           at System.Configuration.ConfigurationManager.PrepareConfigSystem()
           at System.Configuration.ConfigurationManager.GetSection(String sectionName)
           at System.Configuration.ConfigurationManager.get_AppSettings()
           at System.Data.Entity.Core.Objects.ObjectContext..ctor(EntityConnection connection, Boolean isConnectionConstructor, ObjectQueryExecutionPlanFactory objectQueryExecutionPlanFactory, Translator translator, ColumnMapFactory columnMapFactory)
           at System.Data.Entity.Infrastructure.DbCompiledModel.<.cctor>b__2(EntityConnection c)
           at System.Data.Entity.Infrastructure.DbCompiledModel.CreateObjectContext[TContext](DbConnection existingConnection)
           at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
           at System.Data.Entity.Internal.InternalContext.Initialize()
           at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
           at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
           at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
           at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
           at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)

and so on.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,343 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,241 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 47,711 Reputation points
    2021-02-04T15:05:37.11+00:00

    If literally nothing has changed, including framework updates, then I'd say it is an intermittent server issue. Your app had just started to create an instance of your KBDbContext class. That would trigger a call to the base DbContext class which needed something from the config file so that triggered the loading of the configuration subsystem. Early in the process it creates an instance of ClientConfigurationSystem to host the configuration and within there it is ultimately creating an instance of ClientConfigurationHost and then calling the property IsAppConfigHttp which tries to fetch the machine's exe configuration. It is inside here that you get a null reference exception. Scanning this code it doesn't look like there is any path that would cause a null reference exception as everything is properly initialized or would throw an exception.

    I would say it is a quirk and just monitor. If it happens again then there could be a problem otherwise it's gremlins.