I am trying to add a binding to IIS 8.5 using the ServerManager class, but it always results in the application restarting. The site is running on IIS 8.5, running ASP.NET 4.5 and is configured to run in integrated mode. Previously, when my developer machine was running windows 7 and we were using Asp.Net 3.5 this did not happen. I want to be able to add new bindings without the application restarting. Here's part of the code. The recycle occurs when the CommitChanges() method is called, but without it the bindings are not added. The recycle is said to be because of the configuration change, but I have the AppPool's "Disable Recycling for Configuration Change" set to True, which I had hoped would prevent this behaviour.
Logging the stacktrace from Global.Application_End() tells me this:
_shutDownMessage=IIS configuration change HostingEnvironment initiated shutdown HostingEnvironment caused shutdown
_shutDownStack= at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo) at System.Environment.get_StackTrace() at System.Web.Hosting.HostingEnvironment.InitiateShutdownInternal() at System.Web.Hosting.PipelineRuntime.StopProcessing()
using (ServerManager mgr = new ServerManager())
{
foreach (var site in mgr.Sites)
{
if (Convert.ToString(site.Name.ToLowerInvariant()) == siteName)
{
bool bindingExists = false;
foreach (var binding1 in site.Bindings)
{
if (binding1.Host == domainName &&
Convert.ToString(binding1.EndPoint.Port) == portNo)
{
bindingExists = true;
result = true;
break;
}
}
if (!bindingExists)
{
string bind = "*:" + portNo + ":" + domainName;
Binding binding = site.Bindings.CreateElement();
binding.Protocol = "http";
binding.BindingInformation = bind;
site.Bindings.Add(binding);
mgr.CommitChanges();
result = true;
}
break;
}
}
}