Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing <configSections> in config file after deployment

Update: I had a question below but actually my problem would be solved by asking a slightly different question. Why on some machines does my application throw the error:

Configuration system failed to initialize - System.Configuration -    at     System.Configuration.ConfigurationManager.PrepareConfigSystem()

where as on other machines it does not. The error as described also here .NET 3.5 - Configuration system failed to initialize exception is caused by a missing configSections element at the top of my app.config. Of course, the problem would be resolved by putting this section in but for some reason the app.config in my projects solution which has this section is not the one which gets created in the appdata folder once it is deployed.

Original Question:

Why would my user config file be missing this section when deployed on some machines and not others? How can I ensure it is not missing.

<configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="NameOfAddin_Add_in.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
</configSections>

Some background. I'm deploying a vsto add-in through a click once visual studio installer on win 7 machines running outlook 2007 and 2010.

The add-in reads and writes some settings to the app.config file which unlike an exe gets stored in the local users appdata folder.

On some machines however I'm getting an error "Configuration system failed to initialize - System.Configuration - at System.Configuration.ConfigurationManager.PrepareConfigSystem()" which in my case is being caused by the above missing element in the xml. However on other machines the configSections is not missing. The problem is unrelated to the Outlook version being used.

like image 767
user48408 Avatar asked Mar 26 '26 10:03

user48408


1 Answers

I've had the same issue in my VSTO DLL project yesterday and I still don't understand the reason why the with name="userSettings" is sometimes missing. But I can provide my solution: I've made a function which copies the missing XML part (if it's missing) from the fixed ".dll.config" file to the config file in the ROAMING directory:

    /// <summary>
    /// Corrects the roaming settings file if needed because sometimes the node "configSections" is missing in the settings file. 
    /// Correct this by taking this node out of the default config file.
    /// </summary>
    private static void CorrectRoamingSettingsFileIfNeeded()
    {
        const string NODE_NAME_CONFIGURATION = "configuration";
        const string NODE_NAME_CONFIGSECTIONS = "configSections";
        const string NODE_NAME_USERSETTINGS = "userSettings";
        const string ADDIN_DLL_FILENAME = "MyAddIn.dll";

        //Exit if no romaing config (file) to correct...
        var configRoaming = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming);
        if (configRoaming == null) return;
        if (!configRoaming.HasFile) return;

        //Check for the <sectionGroup> with name="userSettings"
        //Note: Used ugly iteration because "configRoaming.GetSectionGroup(sectionGroupName)" throws ArgumentException.
        ConfigurationSectionGroup sectionGroupUserSettings = null;
        foreach (ConfigurationSectionGroup sectionGroup in configRoaming.SectionGroups)
        {
            if (sectionGroup.Name.Equals(NODE_NAME_USERSETTINGS))
            {
                sectionGroupUserSettings = sectionGroup;
                break;
            }
        }

        //Exit if the needed section group is found...
        if (sectionGroupUserSettings != null && sectionGroupUserSettings.IsDeclared) return;

        //Do correction actions...
        var xDoc = XDocument.Load(configRoaming.FilePath);
        var userSettingsNode = xDoc.Element(NODE_NAME_CONFIGURATION).Element(NODE_NAME_USERSETTINGS);

        var addInDllConfigFullFilename = AppDomain.CurrentDomain.BaseDirectory + ADDIN_DLL_FILENAME;
        var configDefault = ConfigurationManager.OpenExeConfiguration(addInDllConfigFullFilename);
        var xDocDefault = XDocument.Load(configDefault.FilePath);
        var configSectionsNode = xDocDefault.Element(NODE_NAME_CONFIGURATION).Element(NODE_NAME_CONFIGSECTIONS);

        userSettingsNode.AddBeforeSelf(configSectionsNode);
        xDoc.Save(configRoaming.FilePath);
    }
like image 187
jreichert Avatar answered Mar 28 '26 00:03

jreichert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!