0
語境
我使用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
它的工作原理,謝謝!沒有辦法檢索事件「whenStopped」重新啓動網站? –
你會這樣想,但我對這方面的知識相當有限。 –