2

I am having an issue with an asp.net app where the web.config is updated and reloaded dynamically.

The following code is used to save the appsettings section in web.config:

     public static void SetWebConfigValue(string keyValue, string value, bool isDefault)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");

                AppSettingsSection appSettings = config.AppSettings;

                KeyValueConfigurationCollection settings = appSettings.Settings;

                if (KeyExists(settings.AllKeys, keyValue))
                {
                    settings.Remove(keyValue);
                }

                if (!isDefault) settings.Add(keyValue, value);

                config.Save(ConfigurationSaveMode.Modified, true);
            }
        }

This code works just fine when I deploy the web app using on a machine with the .net 4.0 runtime. However, on a machine with the .net 4.7 runtime installed, an extra (empty) <System.Windows.Forms.ApplicationConfigurationSection /> is added to web.config. Since this setting cannot be empty, this crashes the web app when the web.config is reloaded.

Edit: Question: why does the latest .net 4.7 adds an invalid configsection to my web.config?

Edit 2: the following issue has been created: https://github.com/Microsoft/dotnet/issues/435

BigONotation
  • 4,406
  • 5
  • 43
  • 72

1 Answers1

1

We ran into this as well.

I think it looks like Microsoft missed the backwards compatibility here between .Net 4.7 and earlier versions.

You didn't really pose this as a question, but what we ended up doing was reading the config file as a string after saving it, then we removed the offending section with a string replacement and saved the string down to file again. It's not pretty but it gets the job done.

Bub
  • 80
  • 1
  • 7
  • 1
    OK thanks for confirming that you are also having this problem. I have filed this as an issue: https://github.com/Microsoft/dotnet/issues/435 – BigONotation Jul 04 '17 at 18:06