title: SPWebConfigModification class will not update Central Admin application's web.config file
body: <p>Using SharePoint 2010 I am trying to use the SPWebConfigModification class to make some basic changes to web.config files on applications in the farm, including the Central Administration web.config file using web application scoped features with feature receivers handling the addition/removal of the modifications:</p>
  
  <pre><code>public override void FeatureActivated(SPFeatureReceiverProperties properties)
      {
          SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
          String ownerId = properties.Feature.DefinitionId.ToString();
          List&lt;SPWebConfigModification&gt; modsToAdd = new List&lt;SPWebConfigModification&gt;();
  
          #region Authentication Providers
  
          modsToAdd.Add(new SPWebConfigModification()
          {
              Name = "defaultProvider",
              Owner = ownerId,
              Path = "configuration/system.web/membership",
              Sequence = 0,
              Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute,
              Value = "FBA_AD_MP"
          });
  
          modsToAdd.Add(new SPWebConfigModification()
          {
              Name = "add [@name=\"FBA_AD_MP\"] [@type=\"System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\"] [@connectionStringName=\"ADFBAConnectionString\"] [@enableSearchMethods=\"true\"] [@attributeMapUsername=\"userPrincipalName\"]",
              Owner = ownerId,
              Path = "configuration/system.web/membership/providers",
              Sequence = 0,
              Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
              Value = "&lt;add name=\"FBA_AD_MP\" type=\"System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\" connectionStringName=\"ADFBAConnectionString\" enableSearchMethods=\"true\" attributeMapUsername=\"userPrincipalName\" /&gt;"
          });
  
          #endregion
  
          #region Connection Strings
  
          modsToAdd.Add(new SPWebConfigModification()
          {
              Name = "connectionStrings",
              Owner = ownerId,
              Path = "configuration",
              Sequence = 0,
              Type = SPWebConfigModification.SPWebConfigModificationType.EnsureSection,
              Value = "&lt;connectionStrings /&gt;"
          });
  
          modsToAdd.Add(WebConfigModificationsUtility.CreateConnectionStringModification(ownerId, 1, "ADFBAConnectionString", properties.Feature.Properties["ADFBAConnectionString"].Value));
  
          #endregion
  
          WebConfigModificationsUtility.AddWebConfigModifications(webApp, modsToAdd.ToArray());
      }
  </code></pre>
  
  <p>The WebConfigModificationsUtility class is a simple utility shared by these feature receivers:</p>
  
  <pre><code>public static void AddWebConfigModifications(SPWebApplication webApp, params SPWebConfigModification[] modificationsToAdd)
      {
          AddWebConfigModifications(webApp, true, modificationsToAdd);
      }
  public static void AddWebConfigModifications(SPWebApplication webApp, Boolean persistChanges, params SPWebConfigModification[] modificationsToAdd)
      {
          foreach (SPWebConfigModification curMod in modificationsToAdd)
          {
              SPWebService.ContentService.WebApplications[webApp.Id].WebConfigModifications.Add(curMod);                
          }
  
          if (persistChanges)
          {
              PersistWebConfigModifications(webApp);
          }
      }
  public static void PersistWebConfigModifications(SPWebApplication webApp)
      {
          SPWebService.ContentService.WebApplications[webApp.Id].Update();                        
          SPWebService.ContentService.ApplyWebConfigModifications();
      }
  </code></pre>
  
  <p>Everything is working fine on content applications, but when attempting to activate features on the central admin web application, the SPWebConfigModificaiton items are never written to the web.config files. I have verified that the code is executing without exceptions. Additionally, my feature receiver attempts to remove any modifications is had made on deactivation:</p>
  
  <pre><code>public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
          {
              SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
              String ownerId = properties.Feature.DefinitionId.ToString();
              WebConfigModificationsUtility.RemoveWebConfigModifications(webApp, ownerId);
          }
  public static void RemoveWebConfigModifications(SPWebApplication webApp, String ownerId, Boolean persistChanges)
          {            
              RemoveWebConfigModifications(webApp, persistChanges, webApp.WebConfigModifications.Where(x =&gt; x.Owner == ownerId).ToArray());
          }
  public static void RemoveWebConfigModifications(SPWebApplication webApp, Boolean persistChanges, params SPWebConfigModification[] modificationsToRemove)
          {
              foreach (SPWebConfigModification curMod in modificationsToRemove)
              {
                  SPWebService.ContentService.WebApplications[webApp.Id].WebConfigModifications.Remove(curMod);                
              }
  
              if (persistChanges)
              {
                  PersistWebConfigModifications(webApp);
              }
          }
  </code></pre>
  
  <p>When this code runs on the Central Admin web application it finds the four modifications that were created in the feature activation and successfully removes them, but <em>neither</em> of these operations is actually changing the web.config file of the web application; it is not even touched, the time stamp for last edited date remains the same throughout.</p>
  
  <p>I have found various blogs talking about the inherit trickiness of the SPWebConfigModification class, with most problems being around properly using the Name and Owner properties, as well as properly persisting changes using the SPWebService static properties, but as best I can tell I'm following all recommended practices. I'm beginning to suspect there's a bug in the API, but I'm wondering if anyone has done this in SharePoint 2010 successfully or if I should crack open Reflector and try to see what gives?</p>