简体   繁体   中英

Add Binding To IIS Without Recycling Application

We have an application that allows us to associate multiple URLs and we want to allow our customers to add new URLs themselves, we have the following code which successfully adds the bindings:

  using (ServerManager serverManager = new ServerManager())
  {
    var site = serverManager.Sites["SITENAME"];
    var binding = site.Bindings.SingleOrDefault(x => x.Host == hostName);
    if (binding == null)
    {
      binding = site.Bindings.CreateElement("binding");
      binding["protocol"] = "http";
      string ip = "*";
      string port = "80";
      binding["bindingInformation"] = string.Format(@"{0}:{1}:{2}", ip, port, hostName);

      site.Bindings.Add(binding);
      serverManager.CommitChanges();
    }
  }

The issue we have is that this causes the application to recycle, I have tested this on our solution using local IIS and the Appliation_Start does get hit again. I have also manually added a binding from IIS which also causes an recycle. While I suspect the answer is no is their a solution to adding a binding programatically without causing the application to recycle?

You can try to change the setting Disable Recycling for Configuration Changes on the application pool that is hosting your site.

In PowerShell:

Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.applicationHost/applicationPools/add[@name='MyPool']/recycling" -name "disallowRotationOnConfigChange" -value "True"

A quick test here showed that adding/removing bindings didn't unload the current AppDomain anymore after changing that setting.

Even if that works, you want to test this for a while to make sure it doesn't break anything else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM