0

语境

我使用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
4

1 回答 1

3

我认为这是一个时间问题。当您使用 IIS 管理控制台停止站点并随后尝试启动它时,情况也是如此。你必须给它一些时间来完成停止。所以(警告:丑陋的)睡眠可能会:

System.Threading.Thread.Sleep(5000);

它在调试时起作用的事实是因为您在继续之前给了它延迟。

于 2012-08-27T12:46:57.710 回答