Thursday, 9 June 2011

IIS7 Site Administration

IIS 7 Site creation and administration has never been easier with the new IIS7 management API. Like the managed Directory services API (System.DirectoryServices.AccountManagement) this library has saved me a lot of time. Here is a code snippet to show you how to create a site, its application, pool and root virtual folder.

Add a reference to the Microsoft.Web.Administration assembly.
Note:
1) Make sure you have IIS7 Features turned on for this library to be available (Windows features).
2) Ensure that you have a reference to System.ServiceModel.
3) Ensure that your target framework is .Net Framework 4.
Location:
C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll

Resources:
http://blogs.msdn.com/b/carlosag/archive/2006/04/17/microsoftwebadministration.aspx
http://learn.iis.net/page.aspx/165/how-to-use-microsoftwebadministration/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Web.Administration;
using System.IO;
using System.Diagnostics;

namespace IIS7Administration
{
    class Program
    {
        static void Main(string[] args)
        {

            using (ServerManager manager = new ServerManager())
            {

                string siteName = "TestSite";
                string poolName = "TestSitePool";

                try
                {
                    Site site = manager.Sites[siteName];

                    if (site == null)
                    {
                        //create the site
                        site = manager.Sites.CreateElement();
                        site.Id = 1;

                        if (manager.Sites.Count > 0)
                            site.Id = manager.Sites.Max(s => s.Id) + 1;

                        site.ApplicationDefaults.EnabledProtocols = "http,net.tcp";
                        site.SetAttributeValue("name", siteName);
                        manager.Sites.Add(site);

                        // create the site's application
                        Application app = site.Applications.CreateElement();
                        app.SetAttributeValue("path", "/");
                        app.SetAttributeValue("applicationPool", poolName);
                        app.EnabledProtocols = "http,net.tcp";
                        site.Applications.Add(app);

                        // create the sites root virtual directory
                        VirtualDirectory vdir = app.VirtualDirectories.CreateElement();
                        vdir.SetAttributeValue("path", "/");

                        string sitePath = Path.Combine(@"c:\inetpub\wwwroot", siteName);
                        if (!Directory.Exists(sitePath))
                            Directory.CreateDirectory(sitePath);

                        vdir.SetAttributeValue("physicalPath", sitePath);

                        app.VirtualDirectories.Add(vdir);

                    }

                    // Create the default site pool
                    ApplicationPool apppool = manager.ApplicationPools[poolName];

                    // Create the application pool if necessary
                    if (apppool == null)
                    {
                        apppool = manager.ApplicationPools.Add(poolName);
                        Debug.WriteLine("Creating new application pool : " + poolName);
                    }

                    string appPoolUserName = "jean";
                    string appPoolUserPass = "test";

                    // set the pool's identity if necessary
                    apppool.ProcessModel.IdentityType = ProcessModelIdentityType.SpecificUser;
                    apppool.ProcessModel.UserName = appPoolUserName;
                    apppool.ProcessModel.Password = appPoolUserPass;

                    // set the pool's runtime version & pipeline mode
                    apppool.ManagedRuntimeVersion = "v4.0";
                    apppool.ManagedPipelineMode = ManagedPipelineMode.Integrated;
                    apppool.AutoStart = true;

                    // setup the bindings for the site
                    Binding httpBinding = (from b in site.Bindings
                                           where b.Protocol == "http"
                                           select b).FirstOrDefault<Binding>();

                    if (httpBinding == null)
                    {
                        httpBinding = site.Bindings.CreateElement();
                        httpBinding.SetAttributeValue("protocol", "http");
                        httpBinding.BindingInformation = "*:" + Convert.ToString("9090") + ":";
                        site.Bindings.Add(httpBinding);
                    }

                    // commit the changes to IIS 7
                    manager.CommitChanges();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Unable to commit changes. Reason:" + ex.Message);
                }
            }

        }
    }
}