简体   繁体   中英

Setting Server Bindings of IIS 6.0 Programmatically

I'm trying to set up an installer to register a web site. Currently, I've got it creating an Application Pool and Web Site under Windows Server 2003. Unfortunately, whenever I try to modify the ServerBindings property to set the IP Address, it throws an exception at me. I first tried this because the documentation here told me to http://msdn.microsoft.com/en-us/library/ms525712%28VS.90%29.aspx . I'm currently using VB.NET, but C# answers are okay too as I need to switch it over to using C# anyway.

siteRootDE.Properties.Item("ServerBindings").Item(0) = "<address>"

This throws an ArgumentOutOfRangeException. I checked it, and server bindings is of size 0. When I tried to create a new entry in the list like this:

siteRootDE.Properties.Item("ServerBindings").Add("<address>")

I get a COMException when I try that.

I looked at the registered property keys, and ServerBindings is nowhere to be found. However, when I create the Web Site through IIS, it generates ServerBindings correctly and I can see it.

What do I need to do to get ServerBindings to appear?

EDIT: I moved the code over to C# and tried it. It seems for some reason, VB.NET crashes when given either the above, but C# doesn't. But that code still doesn't seem to do anything. It just silently fails. I'm trying it like this:

// WebPage is the folder where I created the website
DirectoryEntry siteRootDE = new DirectoryRoot("IIS://LocalHost/W3SVC/WebPage");
// www.mydomain.com is one of the IP addresses that shows up
// when I used the IIS administrative program
siteRootDE.Properties["ServerBindings"].Value = ":80:www.mydomain.com";
siteRootDE.CommitChanges();

In C# you should be able to do this:

webSite.Invoke("Put", "ServerBindings", ":80:www.mydomain.com"); 

or

webSite.Properties["ServerBindings"].Value = ":80:www.mydomain.com";

EDIT:

Here is the sample code I used.

public static void CreateNewWebSite(string siteID, string hostname)
{
    DirectoryEntry webService = new DirectoryEntry("IIS://LOCALHOST/W3SVC");

    DirectoryEntry website = new DirectoryEntry();
    website = webService.Children.Add(siteID, "IIsWebServer");
    website.CommitChanges();

    website.Invoke("Put", "ServerBindings", ":80:" + hostname);
    // Or website.Properties["ServerBindings"].Value = ":80:" + hostname;            
    website.Properties["ServerState"].Value = 2;
    website.Properties["ServerComment"].Value = hostname;
    website.CommitChanges();

    DirectoryEntry rootDir = website.Children.Add("ROOT", "IIsWebVirtualDir");
    rootDir.CommitChanges();

    rootDir.Properties["AppIsolated"].Value = 2;
    rootDir.Properties["Path"].Value = @"C:\Inetpub\wwwroot\MyRootDir";
    rootDir.Properties["AuthFlags"].Value = 5;
    rootDir.Properties["AccessFlags"].Value = 513;
    rootDir.CommitChanges();
    website.CommitChanges();
    webService.CommitChanges();
}

Also, here is a good article for reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM