我試圖自動化IIS應用程序創建,併爲此我使用Microsoft.Web.Administration庫。下面是部分代碼我使用:通過使用Microsoft.Web.Administration庫創建IIS應用程序創建兩個應用程序而不是一個
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下的網站和適當的應用程序池,但是當我在新創建的應用程序池的菜單中選擇「查看應用程序」時,我看到有兩個應用程序,但我是sur e必須只有一個應用程序。我正在努力避免創建兩個應用程序,但找不到方法。這裏是一個圖片說明是:
因此,這裏是我的問題:爲什麼經過上述代碼執行兩個應用程序的創建,以及如何修改代碼以避免這個問題?
GetSite&GetApplication在哪裏引用? – thestar 2014-07-29 16:05:06
public static Site GetSite(ServerManager serverManager,string siteName) { return serverManager.Sites.FirstOrDefault(x => x.Name == siteName); } 私有靜態應用GetApplication( ServerManager的ServerManager的, 串SITENAME, 串的applicationName) { 返回serverManager.Sites .FirstOrDefault(X => x.Name == SITENAME)?應用 .FirstOrDefault(X => x.Path ==「/」+ applicationName); } – 2017-08-10 03:29:45