Set website port with ServerManager class

℡╲_俬逩灬. 提交于 2019-12-24 07:49:26

问题


I need help setting a port for a website using the Microsoft.Web.Administration.ServerManager class.

First I get the website:

Site site = this._serverManager.Sites[section.WebsiteName];

Then I try to set the port from my settings I passed in:

foreach (Binding b in from binding in site.Bindings.Where(b => b != null && b.EndPoint != null)
                                  select binding)
            {
                b.EndPoint.Port = Int32.Parse(section.Port);
                Console.WriteLine(b.EndPoint.Port);
            }    this._serverManager.CommitChanges();

I put the writeline in there to check and the port never gets changed why? I already know the website is valid because I check that before I get here.

Binding binding = site.Bindings.CreateElement();
                binding.BindingInformation = String.Format("{2}:{0}:{1}", section.Port,b.Host, b.EndPoint.Address);
                //b.EndPoint.Port = Int32.Parse(section.Port);
                site.Bindings.Add(binding);

I tried that above and I get a COMException about GetAttributeValue.

Finally got it thanks to the answer below I had to do:

b.BindingInformation = String.Format("{2}:{0}:{1}", section.Port, b.Host, b.EndPoint.Address);

回答1:


Do you commit your changes?

This is the code I have in my server management app (created from data in a Xml document):

ServerManager manager = new ServerManager();
Site site = manager.Sites[siteName];

foreach (XElement bindingNode in bindingsNode.Elements("Binding")) {
    Binding binding = site.Bindings.CreateElement();
    binding.BindingInformation = String.Format("{2}:{0}:{1}", bindingNode.Attribute("Port").Value, bindingNode.Value, bindingNode.Attribute("IP").Value);
    site.Bindings.Add(binding);
}

manager.CommitChanges();


来源:https://stackoverflow.com/questions/11616606/set-website-port-with-servermanager-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!