title: Resolve 'configuration object is read only, because it has been committed by a call to ServerManager.CommitChanges()'?
body: <p>I've written a custom action for an installer project that does the following:</p>
  
  <ul>
  <li>Checks existing websites to see if any exist with the same name put
  in by the user.</li>
  <li>Creates the website in IIS if it doesn't exist.</li>
  <li>Creates an application pool.</li>
  <li>Assigns the application pool to the created website.</li>
  </ul>
  
  <p>When it comes to assigning the application pool I get and error:</p>
  
  <blockquote>
    <p>The configuration object is read only, because it has been committed
    by a call to ServerManager.CommitChanges(). If write access is
    required, use ServerManager to get a new reference.</p>
  </blockquote>
  
  <p>This baffles me as it seems to suggest that I can't assign the newly created application pool with the ServerManager.CommitChanges() call. However, everything else works fine using this, which I wouldn't expect if this was an issue.</p>
  
  <p>Here is my code:</p>
  
  <p>I have a ServerManager instance created like so:</p>
  
  <pre><code>private ServerManager mgr = new ServerManager();
  </code></pre>
  
  <p>In my Install method I do the following:</p>
  
  <pre><code>Site site = CreateWebsite();
  if (site != null)
  {
      CreateApplicationPool();
      AssignAppPool(site);
  }
  </code></pre>
  
  <p><strong>Check existing websites - done in OnBeforeInstall method</strong></p>
  
  <pre><code>    private Site CheckWebsites()
      {
          SiteCollection sites = null;
          Site site = null;
  
          try
          {
              sites = mgr.Sites;
              foreach (Site s in sites)
              {
                  if (!string.IsNullOrEmpty(s.Name))
                  {
                      if (string.Compare(s.Name, targetSite, true) == 0) site = s;
                  }
              }
          }
          catch{}
  
          return site;
      }
  </code></pre>
  
  <p><strong>CreateWebSite method</strong>:</p>
  
  <pre><code>    private Site CreateWebsite()
      {
          Site site = CheckWebsites();
  
          if (site == null)
          {
              SiteCollection sites = mgr.Sites;
              int port;
              Int32.TryParse(targetPort, out port);
              site = sites.Add(targetSite, targetDirectory, port);
              mgr.CommitChanges();
          }
          else
          {
              //TO DO - if website already exists edit settings
          }
  
          return site;
      }
  </code></pre>
  
  <p><strong>Create App Pool</strong></p>
  
  <pre><code>//non-relevant code...
  
  ApplicationPool NewPool = mgr.ApplicationPools.Add(ApplicationPool);
  NewPool.AutoStart = true;
  NewPool.ManagedRuntimeVersion = "4.0"; 
  NewPool.ManagedPipelineMode = ManagedPipelineMode.Classic;
  mgr.CommitChanges();
  </code></pre>
  
  <p><strong>Assign App Pool</strong></p>
  
  <pre><code>private void AssignAppPool(Site site)
  {
      site.ApplicationDefaults.ApplicationPoolName = ApplicationPool; //ERRORS HERE
      mgr.CommitChanges();
  }
  </code></pre>
  
  <p>I can't see why a site could be created, an app pool created but then not assigned. Help.</p>