我使用ServerManager
类在网站上执行某些操作。
实际上,我在site1
上删除了绑定(http / https),并在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
答案 0 :(得分:3)
我认为这是一个时间问题。当您使用IIS管理控制台停止站点并随后尝试启动它时,情况也是如此。你必须给它一些时间来完成停止。所以(警告:丑陋)睡眠可能会这样做:
System.Threading.Thread.Sleep(5000);
它在调试时有效,这是因为你在继续之前给它延迟了。