语境
我使用ServerManager
该类在站点上执行一些操作。
实际上,我删除了 a 上的绑定(http/https),site1
然后在site2
.
我用.CommitChanges()
.
之后,问题是 IIS 关闭了该站点。所以我用site2.Start()
重新启动站点。
但这不起作用......除非我在site2.Start()
.
知道发生了什么吗?你有解决方案吗?
完整代码(在最后一行重新启动)
var serverManager = new ServerManager();
var regex = new Regex("^(http|https)://");
var host = regex.Replace(url, "");
var instance = serverManager.Sites.First(site => site.Bindings.Any(binding => binding.Host == host));
var pool = instance.Applications[0].ApplicationPoolName;
var bindingHttp = instance.Bindings.First(attr => attr.Host == host && attr.Protocol == "http");
var bindingHttps = !host.Contains("xxx") ? instance.Bindings.First(attr => attr.Host == host && attr.Protocol == "https") : null;
var bindingHttpInformation = bindingHttp.BindingInformation;
var bindingHttpsInformation = !host.Contains("xxx") ? bindingHttps.BindingInformation : null;
var certificateHash = !host.Contains("xxx") ? bindingHttps.CertificateHash : null;
var certificateStoreName = !host.Contains("xxx") ? bindingHttps.CertificateStoreName : null;
var newSiteName = getPoolOrSiteName(pool);
var newSite = serverManager.Sites.First(attr => attr.Name == newSiteName);
var bindingCollection = newSite.Bindings;
instance.Bindings.Remove(bindingHttp);
if (!host.Contains("xxx"))
{
instance.Bindings.Remove(bindingHttps);
var binding = newSite.Bindings.CreateElement("binding");
binding["protocol"] = "https";
binding["certificateHash"] = certificateHash;
binding["certificateStoreName"] = certificateStoreName;
binding["bindingInformation"] = bindingHttpsInformation;
bindingCollection.Add(binding);
}
var binding2 = newSite.Bindings.CreateElement("binding");
binding2["protocol"] = "http";
binding2["bindingInformation"] = bindingHttpInformation;
bindingCollection.Add(binding2);
serverManager.CommitChanges();
newSite.Start(); //only works if I put a breakpoint here