我正在尝试自动创建 IIS 应用程序,为此我正在使用Microsoft.Web.Administration库。这是我正在使用的部分代码:
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();
}
}
这是调用上述IISHelper类方法的主要代码
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);
执行此代码后,我在 IIS 下成功创建了具有站点和适当应用程序池的应用程序,但是当我在新创建的应用程序池的菜单中选择“查看应用程序”时,我看到了两个应用程序,但我'确定必须只有一个应用程序。我正在努力避免创建两个应用程序,但找不到方法。这是一张图片说明:
所以这是我的问题:为什么在上述代码执行之后创建了两个应用程序以及如何修改代码以避免该问题?