简体   繁体   中英

IIS application creation by using Microsoft.Web.Administration library creates two application instead of one

I'm trying to automate IIS application creation and for this I'm using Microsoft.Web.Administration library. Here is part of code I'm using:

IISHelper.cs

public static void CreateApplicationPool(string applicationPoolName)
{
    using (ServerManager serverManager = new ServerManager())
    {
        if (serverManager.ApplicationPools[applicationPoolName] != null)
            return;
        ApplicationPool newPool = serverManager.ApplicationPools.Add(applicationPoolName);
        newPool.ManagedRuntimeVersion = "v4.0";
        serverManager.CommitChanges();
    }
}

public static void CreateSite(string siteName, string path)
{
    using (ServerManager serverManager = new ServerManager())
    {
        var sites = serverManager.Sites;
        if (sites[siteName] == null)
        {
            sites.Add(siteName, "http", "*:80:", path);
            serverManager.CommitChanges();
        }
    }
}

public static void CreateApplication(string siteName, string applicationName, string path)
{
    using (ServerManager serverManager = new ServerManager())
    {
        var site = GetSite(serverManager, siteName);
        var applications = site.Applications;
        if (applications["/" + applicationName] == null)
        {
            applications.Add("/" + applicationName, path);
            serverManager.CommitChanges();
        }
    }
}

public static void CreateVirtualDirectory(string siteName, string applicationName, string virtualDirectoryName, string path)
{
    using (ServerManager serverManager = new ServerManager())
    {
        var application = GetApplication(serverManager, siteName, applicationName);
        application.VirtualDirectories.Add("/" + virtualDirectoryName, path);
        serverManager.CommitChanges();
    }
}

public static void SetApplicationApplicationPool(string siteName, string applicationPoolName)
{
    using (ServerManager serverManager = new ServerManager())
    {
        var site = GetSite(serverManager, siteName);
        if (site != null)
        {
            foreach (Application app in site.Applications)
            {
                app.ApplicationPoolName = applicationPoolName;
            }
        }                
        serverManager.CommitChanges();
    }
}

And here is main code which calls methods from above mentioned IISHelper class

string siteName = "TestSite";
string applicationName = "TestApp";
string virtualDirectoryName = "TestVirtualDirectory";
string applicationPoolName = "TestAppPool";
string physicalPath = @"C:\inetpub\mynewsite";

IISHelper.CreateApplicationPool(applicationPoolName);
IISHelper.CreateSite(siteName, physicalPath);
IISHelper.CreateApplication(siteName, applicationName, physicalPath);
IISHelper.CreateVirtualDirectory(siteName, applicationName, virtualDirectoryName, physicalPath);
IISHelper.SetApplicationApplicationPool(siteName, applicationPoolName); 

After execution of this code I'm getting successfully created application with site and appropriate application pool under the IIS, but when I'm selecting "View Application" in the menu of newly created application pool I'm seeing there two application, but I'm sure there must be only one application. I'm working on this to avoid two application creation but can't find a way. Here is a picture illustrating that:

在此输入图像描述

So here is my question: Why after the above mentioned code execution two applications are created and how to modify the code to avoid that issue?

Every site must include a "Root Application" which is what you are seeing as the second app. Basically when you request your site above as ' http://example.com/ ' (or whatever host name you set in the bindings) you will be requesting from that root app, in addition you are creating /TestApp which will be requested when calling ' http://example.com/TestApp '. When you create the Site and you provide the physical path it internally creates a root app "/" for it. To see this you can open %windir%\\system32\\inetsrv\\applicationHost.config.

So technically what you are seeing is correct, but you might not actually want a new app? and only need the root app? Do you really need /TestApp to be in the URL?

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