2012-08-27 60 views
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 

回答

3

我認爲這是一個時間問題。當您使用IIS管理控制檯停止站點並隨後嘗試啓動它時,情況也是如此。你必須給它一些時間來完成停止。因此,一個(警告:醜陋的)睡眠或許可以這樣做:

System.Threading.Thread.Sleep(5000); 

,它工作在調試時的事實,是因爲你給它的延遲,然後再繼續。

+0

它的工作原理,謝謝!沒有辦法檢索事件「whenStopped」重新啓動網站? –

+0

你會這樣想,但我對這方面的知識相當有限。 –

相關問題