C #: пытается установить свойство «ServerBindings» моего сайта программно, продолжает падать

2

Я пытаюсь настроить программный IIS программно, следуя шагам по этому руководство msdn

Единственное отличие, которое я сделал, это переключение на winforms вместо консоли. и переменные вместо параметров функции.

однако код генерирует исключение, когда я пытаюсь установить значение singleproperty...

вот мой код..

  string metabasePath = "IIS://localhost/W3SVC/1234", propertyName = "ServerBindings";
  object newValue = " :8080:";

        try
        {

            DirectoryEntry path = new DirectoryEntry(metabasePath);

         //when i try to retrieve the old value,it returns a null 
         PropertyValueCollection propValues = path.Properties[propertyName];

            MessageBox.Show("7"); 
           //the code throws an exception after messagebox,
           //kinda old school debuging 
                path.Properties[propertyName][0] = newValue;

                path.CommitChanges();
                lblerror.Text = "Done";
        }
        catch (Exception ex)
        {
            if ("HRESULT 0x80005006" == ex.Message)
                 lblerror.Text = " Property does not exist at ";
            else
                 lblerror.Text = "Failed in SetSingleProperty "+ ex.Message.ToString();
        }
Теги:
iis
configuration

1 ответ

4
Лучший ответ

Следующие вспомогательные методы (SetServerBinding и RemoveServerBinding) должны быть полезны:

static void Main(string[] args)
{
    using(DirectoryEntry site = new DirectoryEntry("IIS://Localhost/W3SVC/1234"))
    {
        SetServerBinding(":8080:", site);
        RemoveServerBinding(":8080:", site);
        RemoveServerBinding("172.16.4.99:8087:somesite.com", site);
        SetServerBinding("172.16.4.99:8087:somesite.com", site);
    }
}

public static void SetServerBinding(string binding, DirectoryEntry site)
{
    if(site.Properties["ServerBindings"].Contains(binding))
    {
        site.Properties["ServerBindings"].Remove(binding);
        return;
    }

    site.Properties["ServerBindings"].Add(binding);
    site.CommitChanges();
}

public static void RemoveServerBinding(string binding, DirectoryEntry site)
{
    if (site.Properties["ServerBindings"].Contains(binding))
    {
        site.Properties["ServerBindings"].Remove(binding);
    }

    site.CommitChanges();
}

Ещё вопросы

Сообщество Overcoder
Наверх
Меню