Search code examples
c#iisapplication-poolweb-setup-project

How do you modify a Web Setup Project to create a new website, app pool and virtual directory?


I've been reading up on this quite a lot over the last couple of days and there seems to be a lot of information out there on how to create a Web Setup Project and modify it with custom actions etc. I've got so far with this using the following links:

http://weblogs.asp.net/scottgu/tip-trick-creating-packaged-asp-net-setup-programs-with-vs-2005 http://msdn.microsoft.com/en-us/library/ms525598.aspx http://www.dreamincode.net/forums/topic/231074-setup-and-deployment-in-visual-studio-2010/ http://www.codeproject.com/Articles/146626/Extending-Visual-Studio-Setup-Project

However, there doesn't seem to be much on how to specifically modify the User Interface so that the user can create a new site rather than select from the drop down list provided in the 'Installation Address' action (under 'Start'). In addition to that I've come across code in how to create a new application pool, but not how to stop the UI from giving the user the option of selecting 1 from the list first.

At the moment my installer looks likes this:

UI - Installation Address step

As you can see the user currently has 3 fields; Site, Virtual Directory and Application Pool. Site and Application Pool are lists with existing options in them. In the case of the Site drop down list I actually want this to be a text box which allows the user to type in the name of the website they wish to create. For Application Pool I want this to disappear completely as my Custom Action will automatically create an app pool and assign the virtual directory to it (hopefully).

I already have the code (not tested yet) for creating the app pool, virtual directory and assigning it to the app pool but I can't see how to edit the UI for my needs. Do I need to delete the 'Installation Address' step and create my own custom one?

Code below for app pool creation, virtual directory creation and virtual directory assignment to app pool:

//Properties for user input
private string targetSite { get { return this.Context.Parameters["targetsite"]; }}
private string targetVirtualDir { get { return this.Context.Parameters["targetvdir"]; } }
private string targetDirectory { get { return this.Context.Parameters["targetdir"]; } }
private const string ApplicationPool = "TestWebAppAP";
private const string BasePath = "IIS://Localhost/W3SVC/1/Root";
private const string AppPoolsAddress = "IIS://Localhost/W3SVC/AppPools";

//Install
public override void Install(System.Collections.IDictionary stateSaver)
{
    base.Install(stateSaver);

    if (targetSite == null) throw new InstallException("IIS site name not specified");
    else
    {
        CreateApplicationPool();
        CreateVirtualDir();
        AssignVDirtoAppPool();
    }
}

//Create Application Pool
private void CreateApplicationPool()
{
    try
    {
        DirectoryEntry NewPool;
        DirectoryEntry AppPools = new DirectoryEntry(AppPoolsAddress);
        NewPool = AppPools.Children.Add(ApplicationPool, "IIsApplicationPool");
        NewPool.CommitChanges();
    }
    catch (Exception ex)
    {
        MessageBox.Show("Failed to create new application pool \" " + ApplicationPool + "\". Error: " + ex.Message);
        throw new InstallException("Failed to create app pool", ex);
    }
}

//Create Virtual Directory
private void CreateVirtualDir()
{
    try
    {
        DirectoryEntry site = new DirectoryEntry(BasePath);
        string className = site.SchemaClassName.ToString();

        if (className.EndsWith("Server") || className.EndsWith("VirtualDir"))
        {
            DirectoryEntries vdirs = site.Children;
            DirectoryEntry newVDir = vdirs.Add(targetVirtualDir, (className.Replace("Service", "VirtualDir")));
            newVDir.Properties["Path"][0] = targetDirectory;
            newVDir.Properties["AccessScript"][0] = true;
            newVDir.Properties["AppFriendlyName"][0] = targetVirtualDir;
            newVDir.Properties["AppIsolated"][0] = "1";
            newVDir.Properties["AppRoot"][0] = "/LM" + BasePath.Substring(BasePath.IndexOf("/", ("IIS://".Length)));

            newVDir.CommitChanges();
        }
        else
        {
            MessageBox.Show("Failed to create virtual directory. It can only be created in a site or virtual directory node.");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Failed to create virtual directory \"" + targetVirtualDir + "\". Error: " + ex.Message);
        throw new InstallException("Failed to create virtual directory", ex);
    }
}

//Assign virtual directory to app pool
private void AssignVDirtoAppPool()
{
    try
    {
        DirectoryEntry vDir = new DirectoryEntry(BasePath);
        string className = vDir.SchemaClassName.ToString();

        if(className.EndsWith("VirtualDir"))
        {
            object[] param = { 0, ApplicationPool, true };
            vDir.Invoke("AppCreate3", param);
            vDir.Properties["AppIsolated"][0] = "2";
        }
        else
        {
            MessageBox.Show("Failed to assign to application pool. Only virtual directories can be assigned to application pools.");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Failed to assign virtual directory \"" + targetVirtualDir + "\" to application pool \"" + ApplicationPool + "\". Error: " + ex.Message);
        throw new InstallException("Failed to assign virtual directory to application pool", ex);
    }
}

Solution

  • I've found that I can just delete the 'Installation Address' in the UI and make a custom one with textboxes. This seems a little restrictive but the only way I could do it.