tags:

views:

1463

answers:

3

We have been able to create a web site. We did this using the information in this link:

http://msdn.microsoft.com/en-us/library/ms525598.aspx

However, we would like to use a port number other that port 80. How do we do this?

We are using IIS 6

Thanks

Shiraz

A: 
  1. In properties of site select "Web Site" tab and specify TCP Port.
  2. In studio to debug purpose specify http://localhost:<port>/<site> at tab Web for "Use Local IIS Web Server"
Dewfy
Thanks for your reply. But this is how you do it using the UI, I need to know how to do it in code.
Shiraz Bhaiji
Can you use wmi over JScript or VBScript ?
Dewfy
+5  A: 

If you're using IIS 7, there is a new managed API called Microsoft.Web.Administration

An example from the above blog post:

ServerManager iisManager = new ServerManager();
iisManager.Sites.Add("NewSite", "http", "*:8080:", "d:\\MySite");
iisManager.Update();

If you're using IIS 6 and want to do this, it's more complex unfortunately.

You will have to create a web service on every server, a web service that handles the creation of a website because direct user impersonation over the network won't work properly (If I recall this correctly).

You will have to use Interop Services and do something similar to this (This example uses two objects, server and site, which are instances of custom classes that store a server's and site's configuration):

string metabasePath = "IIS://" + server.ComputerName + "/W3SVC";
DirectoryEntry w3svc = new DirectoryEntry(metabasePath, server.Username, server.Password);

string serverBindings = ":80:" + site.HostName;
string homeDirectory = server.WWWRootPath + "\\" + site.FolderName;


object[] newSite = new object[] { site.Name, new object[] { serverBindings }, homeDirectory };

object websiteId = (object)w3svc.Invoke("CreateNewSite", newSite);

// Returns the Website ID from the Metabase
int id = (int)websiteId;

See more here

kitsune
Thanks, unfortunatly we are using IIS 6.
Shiraz Bhaiji
I did this in a legacy maintenance project once, I'll see whether I can find the code. It's pretty arcane and convoluted unfortunately.
kitsune
Thanks. The second set of code you posted worked.
Shiraz Bhaiji
Great... btw watch out for App Pools... there might be need for some additional configuration...
kitsune
A: 

Try the following Code to Know the unUsed PortNo

        DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");

        // Find unused ID PortNo for new web site
        bool found_valid_port_no = false;
        int random_port_no = 1;

        do
        {
            bool regenerate_port_no = false;
            System.Random random_generator = new Random();
            random_port_no = random_generator.Next(9000,15000);

            foreach (DirectoryEntry e in root.Children)
            {
                if (e.SchemaClassName == "IIsWebServer")
                {

                    int site_id = Convert.ToInt32(e.Name);
                    //For each detected ID find the port Number 

                     DirectoryEntry vRoot = new DirectoryEntry("IIS://localhost/W3SVC/" + site_id);
                     PropertyValueCollection pvcServerBindings = vRoot.Properties["serverbindings"];
                     String bindings = pvcServerBindings.Value.ToString().Replace(":", "");
                     int port_no = Convert.ToInt32(bindings);

                     if (port_no == random_port_no)
                    {
                        regenerate_port_no = true;
                        break;
                    }
                }
            }

            found_valid_port_no = !regenerate_port_no;
        } while (!found_valid_port_no);

        int newportId = random_port_no;
Hari