RickStrahl / Westwind.Utilities

A general purpose utility and helper library for .NET development
MIT License
256 stars 61 forks source link

After upgrade from .NET Framework, AppConfiguration causes a NullReferenceException on app startup #28

Closed LyndonGingerich closed 3 months ago

LyndonGingerich commented 3 months ago

I am upgrading a project using AppConfiguration from .NET Framework 4.7.2 to .NET 8.0. After the upgrade, I get this pop-up: image And the app fails to run.

Using the decompiler, I found that my AppConfiguration type is being loaded from applicationConfiguration.json. I store my settings in app.config. applicationConfiguration.json has most of the same field names, but all the booleans are set to false and the strings to null. And so my app, which is expecting configuration to be loaded from XML with "" as the default value for strings instead of null, throws an exception.

What has changed? How should I update my code? I didn't see releases on this repository or release notes on the tags or anything.

LyndonGingerich commented 3 months ago

This is on WestWind.Utilities 3.0.22.

LyndonGingerich commented 3 months ago

Upgrading to WestWind.Utilities 5.0.8 does not change the behavior.

LyndonGingerich commented 3 months ago

This is interesting:


        /// <summary>
        /// Override this method to use a specialized configuration provider for your config class
        /// when no explicit provider is passed to the Initialize() method.
        /// </summary>
        /// <param name="sectionName">Optional section name that was passed to Initialize()</param>
        /// <param name="configData">Optional config data that was passed to Initialize()</param>
        /// <returns>Instance of configuration provider</returns>
        protected virtual IConfigurationProvider OnCreateDefaultProvider(string sectionName, object configData)
        {
            // dynamically construct the generic provider type
#if NETFRAMEWORK
            var providerType = typeof(ConfigurationFileConfigurationProvider<>);
#else
            var providerType = typeof(JsonFileConfigurationProvider<>);
#endif
            var type = GetType();
            Type typeProvider = providerType.MakeGenericType(type);

            var provider = Activator.CreateInstance(typeProvider) as IConfigurationProvider;

            // if no section name is passed it goes into standard appSettings
            if (!string.IsNullOrEmpty(sectionName))
                provider.ConfigurationSection = sectionName;

            return provider;
        }

Maybe the issue is the move from ConfigurationFileConfigurationProvider to JsonFileConfigurationProvider?

LyndonGingerich commented 3 months ago

Hmm. There appears to be some curious use of System.Configuration.NameValueSectionHandler. Maybe that is causing issues.

RickStrahl commented 3 months ago

The old .NET XML provider doesn't work under .NET Core so you have to switch to a different provider - the default provider for .NET Core is the JsonFile provider.

LyndonGingerich commented 3 months ago

All right, I'll migrate. Could we document this? The current documentation seems to be written exclusively for .NET Framework.