twreid
twreid

Reputation: 1453

Set website port with ServerManager class

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);

Upvotes: 1

Views: 3423

Answers (1)

Steen Tøttrup
Steen Tøttrup

Reputation: 3835

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();

Upvotes: 5

Related Questions